关于fetch

来源:互联网 发布:java如何开发流程 编辑:程序博客网 时间:2024/06/18 16:19

打算hql用fetch要注意

Hibernate ignores my outer-join="true" or fetch="join" setting and fetches an association lazily, using n+1 selects!

HQL queries always ignore the setting for outer-join or fetch="join" defined in mapping metadata. This setting applies only to associations fetched usingget() orload(), Criteria queries, and graph navigation. If you need to enable eager fetching for a HQL query, use an explicitLEFT JOIN FETCH.

转自:https://community.jboss.org/wiki/HibernateFAQ-AdvancedProblems?_sscc=t#Hibernate_ignores_my_outerjointrue_or_fetchjoin_setting_and_fetches_an_association_lazily_using_n1_selects

The fetch strategy defined in the mapping document affects:

  • retrieval via get() or load()

  • retrieval that happens implicitly when an association is navigated

  • Criteria queries

  • HQL queries if subselect fetching is used

Irrespective of the fetching strategy you use, the defined non-lazy graph is guaranteed to be loaded into memory. This might, however, result in several immediate selects being used to execute a particular HQL query.

Usually, the mapping document is not used to customize fetching. Instead, we keep the default behavior, and override it for a particular transaction, usingleft join fetch in HQL. This tells Hibernate to fetch the association eagerly in the first select, using an outer join. In theCriteria query API, you would use setFetchMode(FetchMode.JOIN).

上面解释了什么时候fetch strategy有效,还有一般应该override it for a particular transaction, usingleft join fetch in HQL

于是我写了

Query query = getSession().createQuery(" from MessageBox m left join fetch  m.replies where m.id=? "); 


生成了

Hibernate: select messagebox0_.id as id1_0_, replies1_.id as id2_1_, messagebox0_.name as name1_0_, messagebox0_.content as content1_0_, messagebox0_.sendTime as sendTime1_0_, messagebox0_.sendPerson_id as sendPerson5_1_0_, messagebox0_.getPerson_id as getPerson6_1_0_, messagebox0_.collect as collect1_0_, messagebox0_.canBeReplied as canBeRep8_1_0_, messagebox0_.rubbish as rubbish1_0_, replies1_.title as title2_1_, replies1_.content as content2_1_, replies1_.PARENT_REPLY_ID as PARENT4_2_1_, replies1_.MESSAGEBOX_ID as MESSAGEBOX5_2_1_, replies1_.SEND_REPLY_PERSON_ID as SEND6_2_1_, replies1_.SEND_REPLY_TIME as SEND7_2_1_, replies1_.MESSAGEBOX_ID as MESSAGEBOX5_1_0__, replies1_.id as id0__ from MessageBox messagebox0_ left outer join reply replies1_ on messagebox0_.id=replies1_.MESSAGEBOX_ID where messagebox0_.id=?10:04:23,764 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9a129013db9a9eafe000310:04:23,795 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:04:23,812 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [id2_1_]10:04:23,812 TRACE BasicExtractor:71 - found [ffff] as column [name1_0_]10:04:23,827 TRACE BasicExtractor:71 - found [1111] as column [content1_0_]10:04:23,827 TRACE BasicExtractor:71 - found [2013-03-30 12:59:13.331] as column [sendTime1_0_]10:04:23,827 TRACE BasicExtractor:71 - found [2] as column [sendPerson5_1_0_]10:04:23,843 TRACE BasicExtractor:71 - found [1] as column [getPerson6_1_0_]10:04:23,843 TRACE BasicExtractor:71 - found [false] as column [collect1_0_]10:04:23,843 TRACE BasicExtractor:71 - found [false] as column [canBeRep8_1_0_]10:04:23,858 TRACE BasicExtractor:71 - found [false] as column [rubbish1_0_]10:04:23,858 TRACE BasicExtractor:71 - found [zzz] as column [title2_1_]10:04:23,874 TRACE BasicExtractor:71 - found [zzzz] as column [content2_1_]10:04:23,874 TRACE BasicExtractor:66 - found [null] as column [PARENT4_2_1_]10:04:23,874 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:04:23,890 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:04:23,890 TRACE BasicExtractor:71 - found [2013-03-30 14:07:28.882] as column [SEND7_2_1_]10:04:23,890 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_1_0__]10:04:23,905 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [id0__]10:04:23,905 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:04:23,921 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id2_1_]10:04:23,921 TRACE BasicExtractor:71 - found [fffffaaaa] as column [title2_1_]10:04:23,921 TRACE BasicExtractor:71 - found [aaa] as column [content2_1_]10:04:23,936 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [PARENT4_2_1_]10:04:23,936 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:04:23,936 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:04:23,952 TRACE BasicExtractor:71 - found [2013-03-30 21:00:49.149] as column [SEND7_2_1_]10:04:23,952 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_1_0__]10:04:23,968 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id0__]10:04:23,968 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:04:23,968 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id2_1_]10:04:23,983 TRACE BasicExtractor:71 - found [gaga] as column [title2_1_]10:04:23,983 TRACE BasicExtractor:71 - found [aa] as column [content2_1_]10:04:23,983 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [PARENT4_2_1_]10:04:23,999 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:04:23,999 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:04:24,014 TRACE BasicExtractor:71 - found [2013-03-30 21:02:05.133] as column [SEND7_2_1_]10:04:24,014 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_1_0__]10:04:24,030 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id0__]10:04:24,030 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:04:24,030 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id2_1_]10:04:24,046 TRACE BasicExtractor:71 - found [sssss] as column [title2_1_]10:04:24,046 TRACE BasicExtractor:71 - found [ssaasa] as column [content2_1_]10:04:24,046 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [PARENT4_2_1_]10:04:24,061 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:04:24,061 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:04:24,077 TRACE BasicExtractor:71 - found [2013-03-30 14:13:54.344] as column [SEND7_2_1_]10:04:24,077 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_1_0__]10:04:24,077 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id0__]Hibernate: select author0_.id as id0_0_, author0_.name as name0_0_, author0_.profession as profession0_0_, author0_.sex as sex0_0_, author0_.password as password0_0_, author0_.email as email0_0_, author0_.trade as trade0_0_, author0_.worktime as worktime0_0_, author0_.city as city0_0_, author0_.registtime as registtime0_0_, author0_.enable as enable0_0_ from author author0_ where author0_.id=?10:04:24,092 TRACE BasicBinder:82 - binding parameter [1] as [BIGINT] - 210:04:24,092 TRACE BasicExtractor:71 - found [ss] as column [name0_0_]10:04:24,108 TRACE BasicExtractor:71 - found [CTO/CIO/技术总监] as column [profession0_0_]10:04:24,108 TRACE BasicExtractor:71 - found [男] as column [sex0_0_]10:04:24,124 TRACE BasicExtractor:71 - found [111111] as column [password0_0_]10:04:24,124 TRACE BasicExtractor:71 - found [wyxz127@163.com] as column [email0_0_]10:04:24,139 TRACE BasicExtractor:71 - found [金融] as column [trade0_0_]10:04:24,139 TRACE BasicExtractor:71 - found [在读学生] as column [worktime0_0_]10:04:24,139 TRACE BasicExtractor:66 - found [null] as column [city0_0_]10:04:24,155 TRACE BasicExtractor:71 - found [2013-03-25 21:04:24.217] as column [registtime0_0_]10:04:24,155 TRACE BasicExtractor:71 - found [Y] as column [enable0_0_]Hibernate: select author0_.id as id0_0_, author0_.name as name0_0_, author0_.profession as profession0_0_, author0_.sex as sex0_0_, author0_.password as password0_0_, author0_.email as email0_0_, author0_.trade as trade0_0_, author0_.worktime as worktime0_0_, author0_.city as city0_0_, author0_.registtime as registtime0_0_, author0_.enable as enable0_0_ from author author0_ where author0_.id=?10:04:24,170 TRACE BasicBinder:82 - binding parameter [1] as [BIGINT] - 110:04:24,170 TRACE BasicExtractor:71 - found [aa] as column [name0_0_]10:04:24,186 TRACE BasicExtractor:71 - found [CTO/CIO/技术总监] as column [profession0_0_]10:04:24,186 TRACE BasicExtractor:71 - found [男] as column [sex0_0_]10:04:24,202 TRACE BasicExtractor:71 - found [111111] as column [password0_0_]10:04:24,202 TRACE BasicExtractor:71 - found [wyxz125@163.com] as column [email0_0_]10:04:24,202 TRACE BasicExtractor:71 - found [金融] as column [trade0_0_]10:04:24,217 TRACE BasicExtractor:71 - found [在读学生] as column [worktime0_0_]10:04:24,217 TRACE BasicExtractor:66 - found [null] as column [city0_0_]10:04:24,233 TRACE BasicExtractor:71 - found [2013-03-25 20:50:04.199] as column [registtime0_0_]10:04:24,233 TRACE BasicExtractor:71 - found [Y] as column [enable0_0_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:04:24,248 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9d94e013db9ee4ad1000310:04:24,248 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id2_0_]10:04:24,264 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [PARENT4_2_1_]10:04:24,264 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id1_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:04:24,280 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402881e43dbb4d20013dbb63fdf80002Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:04:24,280 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402881e43dbb4d20013dbb62d548000010:04:24,295 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id2_0_]10:04:24,295 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [PARENT4_2_1_]10:04:24,311 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id1_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:04:24,311 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9d94e013db9e86923000210:04:24,326 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id2_0_]10:04:24,326 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [PARENT4_2_1_]10:04:24,342 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id1_]


发现left join fetch 直接把messagebox里面的replies都查询出来了而且.list()只生成4个messagebox,每个messagebox里面的replies都加载了(用messagebox.replies()没有在执行查询语句)

而换用

Query query = getSession().createQuery(" from MessageBox m left join  m.replies where m.id=? "); 


生成了

Hibernate: select messagebox0_.id as id1_0_, replies1_.id as id2_1_, messagebox0_.name as name1_0_, messagebox0_.content as content1_0_, messagebox0_.sendTime as sendTime1_0_, messagebox0_.sendPerson_id as sendPerson5_1_0_, messagebox0_.getPerson_id as getPerson6_1_0_, messagebox0_.collect as collect1_0_, messagebox0_.canBeReplied as canBeRep8_1_0_, messagebox0_.rubbish as rubbish1_0_, replies1_.title as title2_1_, replies1_.content as content2_1_, replies1_.PARENT_REPLY_ID as PARENT4_2_1_, replies1_.MESSAGEBOX_ID as MESSAGEBOX5_2_1_, replies1_.SEND_REPLY_PERSON_ID as SEND6_2_1_, replies1_.SEND_REPLY_TIME as SEND7_2_1_ from MessageBox messagebox0_ left outer join reply replies1_ on messagebox0_.id=replies1_.MESSAGEBOX_ID where messagebox0_.id=?10:22:18,729 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9a129013db9a9eafe000310:22:18,745 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:22:18,762 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [id2_1_]10:22:18,762 TRACE BasicExtractor:71 - found [ffff] as column [name1_0_]10:22:18,762 TRACE BasicExtractor:71 - found [1111] as column [content1_0_]10:22:18,777 TRACE BasicExtractor:71 - found [2013-03-30 12:59:13.331] as column [sendTime1_0_]10:22:18,777 TRACE BasicExtractor:71 - found [2] as column [sendPerson5_1_0_]10:22:18,793 TRACE BasicExtractor:71 - found [1] as column [getPerson6_1_0_]10:22:18,793 TRACE BasicExtractor:71 - found [false] as column [collect1_0_]10:22:18,793 TRACE BasicExtractor:71 - found [false] as column [canBeRep8_1_0_]10:22:18,808 TRACE BasicExtractor:71 - found [false] as column [rubbish1_0_]10:22:18,808 TRACE BasicExtractor:71 - found [zzz] as column [title2_1_]10:22:18,824 TRACE BasicExtractor:71 - found [zzzz] as column [content2_1_]10:22:18,824 TRACE BasicExtractor:66 - found [null] as column [PARENT4_2_1_]10:22:18,824 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:22:18,840 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:22:18,840 TRACE BasicExtractor:71 - found [2013-03-30 14:07:28.882] as column [SEND7_2_1_]10:22:18,855 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:22:18,855 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id2_1_]10:22:18,855 TRACE BasicExtractor:71 - found [fffffaaaa] as column [title2_1_]10:22:18,871 TRACE BasicExtractor:71 - found [aaa] as column [content2_1_]10:22:18,871 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [PARENT4_2_1_]10:22:18,886 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:22:18,886 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:22:18,886 TRACE BasicExtractor:71 - found [2013-03-30 21:00:49.149] as column [SEND7_2_1_]10:22:18,902 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:22:18,902 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id2_1_]10:22:18,918 TRACE BasicExtractor:71 - found [gaga] as column [title2_1_]10:22:18,918 TRACE BasicExtractor:71 - found [aa] as column [content2_1_]10:22:18,933 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [PARENT4_2_1_]10:22:18,933 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:22:18,949 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:22:18,949 TRACE BasicExtractor:71 - found [2013-03-30 21:02:05.133] as column [SEND7_2_1_]10:22:18,949 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [id1_0_]10:22:18,964 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id2_1_]10:22:18,964 TRACE BasicExtractor:71 - found [sssss] as column [title2_1_]10:22:18,964 TRACE BasicExtractor:71 - found [ssaasa] as column [content2_1_]10:22:18,980 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [PARENT4_2_1_]10:22:18,980 TRACE BasicExtractor:71 - found [402841703db9a129013db9a9eafe0003] as column [MESSAGEBOX5_2_1_]10:22:18,996 TRACE BasicExtractor:71 - found [1] as column [SEND6_2_1_]10:22:18,996 TRACE BasicExtractor:71 - found [2013-03-30 14:13:54.344] as column [SEND7_2_1_]Hibernate: select author0_.id as id0_0_, author0_.name as name0_0_, author0_.profession as profession0_0_, author0_.sex as sex0_0_, author0_.password as password0_0_, author0_.email as email0_0_, author0_.trade as trade0_0_, author0_.worktime as worktime0_0_, author0_.city as city0_0_, author0_.registtime as registtime0_0_, author0_.enable as enable0_0_ from author author0_ where author0_.id=?10:22:19,011 TRACE BasicBinder:82 - binding parameter [1] as [BIGINT] - 210:22:19,011 TRACE BasicExtractor:71 - found [ss] as column [name0_0_]10:22:19,027 TRACE BasicExtractor:71 - found [CTO/CIO/技术总监] as column [profession0_0_]10:22:19,027 TRACE BasicExtractor:71 - found [男] as column [sex0_0_]10:22:19,042 TRACE BasicExtractor:71 - found [111111] as column [password0_0_]10:22:19,042 TRACE BasicExtractor:71 - found [wyxz127@163.com] as column [email0_0_]10:22:19,058 TRACE BasicExtractor:71 - found [金融] as column [trade0_0_]10:22:19,058 TRACE BasicExtractor:71 - found [在读学生] as column [worktime0_0_]10:22:19,074 TRACE BasicExtractor:66 - found [null] as column [city0_0_]10:22:19,074 TRACE BasicExtractor:71 - found [2013-03-25 21:04:24.217] as column [registtime0_0_]10:22:19,089 TRACE BasicExtractor:71 - found [Y] as column [enable0_0_]Hibernate: select author0_.id as id0_0_, author0_.name as name0_0_, author0_.profession as profession0_0_, author0_.sex as sex0_0_, author0_.password as password0_0_, author0_.email as email0_0_, author0_.trade as trade0_0_, author0_.worktime as worktime0_0_, author0_.city as city0_0_, author0_.registtime as registtime0_0_, author0_.enable as enable0_0_ from author author0_ where author0_.id=?10:22:19,089 TRACE BasicBinder:82 - binding parameter [1] as [BIGINT] - 110:22:19,105 TRACE BasicExtractor:71 - found [aa] as column [name0_0_]10:22:19,105 TRACE BasicExtractor:71 - found [CTO/CIO/技术总监] as column [profession0_0_]10:22:19,120 TRACE BasicExtractor:71 - found [男] as column [sex0_0_]10:22:19,120 TRACE BasicExtractor:71 - found [111111] as column [password0_0_]10:22:19,136 TRACE BasicExtractor:71 - found [wyxz125@163.com] as column [email0_0_]10:22:19,136 TRACE BasicExtractor:71 - found [金融] as column [trade0_0_]10:22:19,136 TRACE BasicExtractor:71 - found [在读学生] as column [worktime0_0_]10:22:19,152 TRACE BasicExtractor:66 - found [null] as column [city0_0_]10:22:19,152 TRACE BasicExtractor:71 - found [2013-03-25 20:50:04.199] as column [registtime0_0_]10:22:19,167 TRACE BasicExtractor:71 - found [Y] as column [enable0_0_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:22:19,167 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9d94e013db9ee4ad1000310:22:19,183 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id2_0_]10:22:19,183 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [PARENT4_2_1_]10:22:19,198 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [id1_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:22:19,198 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402881e43dbb4d20013dbb63fdf80002Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:22:19,214 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402881e43dbb4d20013dbb62d548000010:22:19,230 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id2_0_]10:22:19,230 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb62d5480000] as column [PARENT4_2_1_]10:22:19,230 TRACE BasicExtractor:71 - found [402881e43dbb4d20013dbb63fdf80002] as column [id1_]Hibernate: select childrenre0_.PARENT_REPLY_ID as PARENT4_2_1_, childrenre0_.id as id1_, childrenre0_.id as id2_0_, childrenre0_.title as title2_0_, childrenre0_.content as content2_0_, childrenre0_.PARENT_REPLY_ID as PARENT4_2_0_, childrenre0_.MESSAGEBOX_ID as MESSAGEBOX5_2_0_, childrenre0_.SEND_REPLY_PERSON_ID as SEND6_2_0_, childrenre0_.SEND_REPLY_TIME as SEND7_2_0_ from reply childrenre0_ where childrenre0_.PARENT_REPLY_ID=?10:22:19,245 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 402841703db9d94e013db9e86923000210:22:19,245 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id2_0_]10:22:19,261 TRACE BasicExtractor:71 - found [402841703db9d94e013db9e869230002] as column [PARENT4_2_1_]10:22:19,261 TRACE BasicExtractor:71 - found [402841703db9d94e013db9ee4ad10003] as column [id1_]


但是注意:

生成了4个object[2]对象, 而每个object[2]里面是messagebox(其中replies没有加载,使用getreplies()继续查询)和reply(其中childrenreplies已加载,点击reply.getChildrenReply()不再查询,由reply的childrenreplies设定)

所以说明 join fetch 直接查询了所有m.replies而只用join则用到replies才查询。
原创粉丝点击