终于搞点了CREATESQLQUERY了
CREATESQLQUERY最好用原生的SQL
如
Query qu =session.createSQLQuery("select user_account.NID, user_archive.* from user_account left outer join user_archive on user_account.NID = user_archive.NUSER_ID where user_account.NID = "+userId);
以上都是表名同列名 没有对应的类同属性
如果一定要用对应的类同属性名是记的要给别名还有就是用了别名一定要给这个别名下定义如
Query qu =session.createSQLQuery("select NID, {b.*} from user_account , user_archive {b} where NID = {b}.NUSER_ID and NID = "+userId).addEntity("b", UserArchive.class).addScalar("NID",new LongType());
以上的除了表名外还有的是属性名记的用别名引用属性名是一定要用addEntity("b", UserArchive.class)不然会出错:好像172202错吧说别名不识别
CREATESQLQUERY中可以直接进行多表操作不用在配置中配置MANYTOONE OR ONETOONE OR MANYTOMANY 如:
Query qu =session.createSQLQuery("select NID , {b.*} from user_account left outer join user_archive {b} on NID = {b}.NUSER_ID where NID = "+userId).addEntity("b", UserArchive.class).addScalar("NID",new LongType());
Query qu =session.createSQLQuery("select user_account.NID, user_archive.* from user_account left outer join user_archive on user_account.NID = user_archive.NUSER_ID where user_account.NID = "+userId);
对于投影查询使用别名的情况要注意了如果你的addEntity()方法加了而只SELECT 一些属性时会出以下错误码:
Hibernate: select NID NID0_1_, b.NUSER_ID as NUSER1_1_0_, b.SNAME as SNAME1_0_, b.SNAME_EN as SNAME3_1_0_, b.SSEX as SSEX1_0_, b.DBIRTH as DBIRTH1_0_, b.SMARRIAGE as SMARRIAGE1_0_, b.NSTATURE as NSTATURE1_0_, b.NAVOIRDUPOIS as NAVOIRDU8_1_0_, b.SCONSTELLATION as SCONSTEL9_1_0_, b.SBLOOD as SBLOOD1_0_, b.SGENUS as SGENUS1_0_, b.SMAXIM as SMAXIM1_0_, b.SCHARACTER as SCHARACTER1_0_, b.SINTEREST as SINTEREST1_0_, b.SCOUNTRY_CODE as SCOUNTRY15_1_0_, b.SAUTOCRITICAL as SAUTOCR16_1_0_, b.NHOMETOWN_CITY_ID as NHOMETOWN17_1_0_, b.NCITY_ID as NCITY18_1_0_, b.SKNOWLEDGE as SKNOWLEDGE1_0_, b.SACADEMY as SACADEMY1_0_, b.SOCCUPATION as SOCCUPA21_1_0_, b.SMONTH_SALARY as SMONTH22_1_0_, b.SEMAIL as SEMAIL1_0_, b.SQQ as SQQ1_0_, b.SMSN as SMSN1_0_, b.SISOPEN_NET_CONTACT as SISOPEN26_1_0_, b.SISOPEN_BASIC_CONTACT as SISOPEN27_1_0_, b.STEL as STEL1_0_, b.SMOBILE as SMOBILE1_0_, b.SADDRESS as SADDRESS1_0_, b.SZIPCODE as SZIPCODE1_0_, b.SPHOTO_URL as SPHOTO32_1_0_ from user_account a , user_archive b where NID = b.NUSER_ID and NID = 15000
- could not read column value from result set: SNICK2_0_1_; 列名无效
ADDENTITY方法照我看是别名的引用同返回对像的说明也就是说用了别名一定要有ADDENTITY但有了ADDENTITY一定会返回整个对像你只SELECT对像中的几个属性就会出错了