) syntax, but for returning non-entity beans it only provided value injection of these beans via its constructor. Thus if you used the same DTO in many different scenarios you could end up having many constructors on this DTO purely for allowing the "select new" functionality to work.
Now you can get the value injected via property methods or fields instead, removing the need for explicit constructors.
List resultWithAliasedBean = s.createQuery( "select e.student.name as studentName," + " e.course.description as courseDescription" + "from Enrolment as e")
.setResultTransformer( Transformers.aliasToBean(StudentDTO.class)) .list();
StudentDTO dto = (StudentDTO) resultWithAliasedBean.get(0);
銆銆
SQL TransformersWith native sql returning non-entity beans or Map's is often more useful instead of basic Object[]. With result transformers that is now possible.
List resultWithAliasedBean = s.createSQLQuery( "SELECT st.name as studentName, co.description as courseDescription " + "FROM Enrolment e " + "INNER JOIN Student st on e.studentId=st.studentId " + "INNER JOIN Course co on e.courseCode=co.courseCode")
.addScalar("studentName") .addScalar("courseDescription")
.setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
.list();
StudentDTO dto =(StudentDTO) resultWithAliasedBean.get(0);
銆銆
Tip: the addScalar() calls were required on HSQLDB to make it match a property name since it returns column names in all uppercase (e.g. "STUDENTNAME"). This could also be solved with a custom transformer that search the property names instead of using exact match - maybe we should provide a fuzzyAliasToBean() method ;)
Map vs. Object[]Since you can also use a transformer that return a Map from alias to value/entity (e.g. Transformers.ALIAS_TO_MAP), you are no longer required to mess with index based Object arrays when working with a result.
List iter = s.createQuery( "select e.student.name as studentName," + " e.course.description as courseDescription" + "from Enrolment as e")
.setResultTransformer( Transformers.ALIAS_TO_MAP )
.iterate();
String name = (Map)(iter.next()).get("studentName");
銆銆
Again, this works equally well for Criteria, HQL and native SQL.
浣跨敤SQLQuery
瀵瑰師鐢烻QL鏌ヨ鎵ц鐨勬帶鍒舵槸閫氳繃SQLQuery鎺ュ彛榪涜鐨勶紝閫氳繃鎵цSession.createSQLQuery()鑾峰彇榪欎釜鎺ュ彛銆傛渶綆鍗曠殑鎯呭喌涓嬶紝鎴戜滑鍙互閲囩敤浠ヤ笅褰㈠紡錛?/p>
List cats = sess.createSQLQuery( " select * from cats " ).addEntity(Cat. class ).list();
榪欎釜鏌ヨ鎸囧畾浜?
SQL鏌ヨ瀛楃涓?/p>
鏌ヨ榪斿洖鐨勫疄浣?/p>
榪欓噷錛岀粨鏋滈泦瀛楁鍚嶈鍋囪涓轟笌鏄犲皠鏂囦歡涓寚鏄庣殑瀛楁鍚嶇浉鍚屻傚浜庤繛鎺ヤ簡澶氫釜琛ㄧ殑鏌ヨ錛岃繖灝卞彲鑳介犳垚闂錛屽洜涓哄彲鑳藉湪澶氫釜琛ㄤ腑鍑虹幇鍚屾牱鍚嶅瓧鐨勫瓧孌點備笅闈㈢殑鏂規硶灝卞彲浠ラ伩鍏嶅瓧孌靛悕閲嶅鐨勯棶棰?
List cats = sess.createSQLQuery( " select {cat.*} from cats cat " ).addEntity( " cat " , Cat. class ).list();
榪欎釜鏌ヨ鎸囧畾浜?
SQL鏌ヨ璇彞錛屽畠甯︿竴涓崰浣嶇錛屽彲浠ヨHibernate浣跨敤瀛楁鐨勫埆鍚?
鏌ヨ榪斿洖鐨勫疄浣擄紝鍜屽畠鐨凷QL琛ㄧ殑鍒悕.
addEntity()鏂規硶灝哠QL琛ㄧ殑鍒悕鍜屽疄浣撶被鑱旂郴璧鋒潵錛屽茍涓旂‘瀹氭煡璇㈢粨鏋滈泦鐨勫艦鎬併?/p>
addJoin()鏂規硶鍙互琚敤浜庤澆鍏ュ叾浠栫殑瀹炰綋鍜岄泦鍚堢殑鍏寵仈.
List cats = sess.createSQLQuery(
" select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id " )
.addEntity( " cat " , Cat. class )
.addJoin( " kitten " , " cat.kittens " )
.list();
鍘熺敓鐨凷QL鏌ヨ鍙兘榪斿洖涓涓畝鍗曠殑鏍囬噺鍊兼垨鑰呬竴涓爣閲忓拰瀹炰綋鐨勭粨鍚堜綋銆?/p>
Double max = (Double) sess.createSQLQuery( " select max(cat.weight) as maxWeight from cats cat " )
.addScalar( " maxWeight " , Hibernate.DOUBLE);
.uniqueResult();
闄ゆ涔嬪錛屼綘榪樺彲浠ュ湪浣犵殑hbm鏂囦歡涓弿榪扮粨鏋滈泦鏄犲皠淇℃伅錛屽湪鏌ヨ涓嬌鐢ㄣ?/p>
List cats = sess.createSQLQuery(
" select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id " )
.setResultSetMapping( " catAndKitten " )
.list();
鍛藉悕SQL鏌ヨ
鍙互鍦ㄦ槧灝勬枃妗d腑瀹氫箟鏌ヨ鐨勫悕瀛?鐒跺悗灝卞彲浠ヨ薄璋冪敤涓涓懡鍚嶇殑HQL鏌ヨ涓鏍風洿鎺ヨ皟鐢ㄥ懡鍚峉QL鏌ヨ.鍦ㄨ繖縐嶆儏鍐典笅,鎴戜滑涓?闇瑕佽皟鐢╝ddEntity()鏂規硶.
< sql - query name = " persons " >
< return alias = " person " class = " eg.Person " />
Select person.NAME AS {person.name},person.AGE AS {person.age},person.SEX AS {person.sex} FROM PERSON person Where person.NAME LIKE :namePattern
</ sql - query >
List people = sess.getNamedQuery( " persons " ).setString( " namePattern " , namePattern)
.setMaxResults( 50 )
.list();

]]>