锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲视频999,国产精品视频免费一区二区三区,黄网在线观看http://www.aygfsteel.com/J2EE/category/32941.htmlzh-cnWed, 01 Jul 2009 21:20:42 GMTWed, 01 Jul 2009 21:20:42 GMT60hibernate涓枃涔辯爜http://www.aygfsteel.com/J2EE/articles/284779.html榪芥ⅵ浜?/dc:creator>榪芥ⅵ浜?/author>Tue, 30 Jun 2009 07:34:00 GMThttp://www.aygfsteel.com/J2EE/articles/284779.htmlhttp://www.aygfsteel.com/J2EE/comments/284779.htmlhttp://www.aygfsteel.com/J2EE/articles/284779.html#Feedback0http://www.aygfsteel.com/J2EE/comments/commentRss/284779.htmlhttp://www.aygfsteel.com/J2EE/services/trackbacks/284779.html   <property name="url"
   value="jdbc:mysql://localhost/struts?useUnicode=true&characterEncoding=gb2312">
  </property>


]]>
hibernate娉ㄩ噴 灞炴т笉鎸佷箙鍖栧埌鏁版嵁搴?/title><link>http://www.aygfsteel.com/J2EE/articles/256460.html</link><dc:creator>榪芥ⅵ浜?/dc:creator><author>榪芥ⅵ浜?/author><pubDate>Tue, 24 Feb 2009 08:37:00 GMT</pubDate><guid>http://www.aygfsteel.com/J2EE/articles/256460.html</guid><wfw:comment>http://www.aygfsteel.com/J2EE/comments/256460.html</wfw:comment><comments>http://www.aygfsteel.com/J2EE/articles/256460.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/J2EE/comments/commentRss/256460.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/J2EE/services/trackbacks/256460.html</trackback:ping><description><![CDATA[@Transient<br /> 璇ユ鐨勶紝鎵句簡涓涓嬪崍錛岀粓浜庢鍑烘潵浜嗭紒 <img src ="http://www.aygfsteel.com/J2EE/aggbug/256460.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/J2EE/" target="_blank">榪芥ⅵ浜?/a> 2009-02-24 16:37 <a href="http://www.aygfsteel.com/J2EE/articles/256460.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>[Hibernate]Hibernate 3.2: Transformers for HQL and SQLhttp://www.aygfsteel.com/J2EE/articles/214048.html榪芥ⅵ浜?/dc:creator>榪芥ⅵ浜?/author>Thu, 10 Jul 2008 09:39:00 GMThttp://www.aygfsteel.com/J2EE/articles/214048.htmlhttp://www.aygfsteel.com/J2EE/comments/214048.htmlhttp://www.aygfsteel.com/J2EE/articles/214048.html#Feedback0http://www.aygfsteel.com/J2EE/comments/commentRss/214048.htmlhttp://www.aygfsteel.com/J2EE/services/trackbacks/214048.htmlResultTransformer. A ResultTransformer is a nice and simple interface that allows you to transform any Criteria result element. E.g. you can make any Criteria result be returned as a java.util.Map or as a non-entity Bean.

Criteria TransformersImagine you have a StudentDTO class:
public class StudentDTO
{  
   private String studentName;  
   private String courseDescription;  
   public StudentDTO() {  }        
   ...
}
Then you can make the Criteria return non-entity classes instead of scalars or entities by applying a ResultTransformer:

List resultWithAliasedBean = s.createCriteria(Enrolment.class)
     .createAlias("student", "st").createAlias("course", "co")
     .setProjection( Projections.projectionList()              
                          .add( Projections.property("st.name"), "studentName" )                  
                          .add( Projections.property("co.description"), "courseDescription" )          )          
     .setResultTransformer( Transformers.aliasToBean(StudentDTO.class) )        
      .list();
StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);  

This is how ResultTransformer have been available since we introduced projection to the Criteria API in Hibernate 3.
It is just one example of the built in transformers and users can provide their own transformers if they so please.
Jealous programming Since I am more a HQL/SQL guy I have been jealous on Criteria for having this feature and I have seen many requests for adding it to all our query facilities.
Today I put an end to this jealousy and introduced ResultTransformer for HQL and SQL in Hibernate 3.2.
HQL TransformersIn HQL we already had a "kind" of result transformers via the ("select new" http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html#queryhql-select) 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();


]]>
主站蜘蛛池模板: 屏东县| 嘉禾县| 涿州市| 开远市| 井陉县| 潮州市| 金湖县| 西林县| 北宁市| 八宿县| 贡山| 二手房| 济阳县| 西华县| 宁陕县| 太仓市| 黄浦区| 望奎县| 青冈县| 古交市| 曲松县| 和静县| 崇州市| 平顶山市| 西峡县| 神池县| 伊宁市| 宜城市| 镇赉县| 油尖旺区| 遂宁市| 新宾| 临沭县| 宁河县| 金乡县| 东乡族自治县| 黎平县| 舒兰市| 安阳县| 炎陵县| 贵定县|