-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Redundant sqls after join fetch
PostPosted: Thu Mar 16, 2006 8:00 am 
Newbie

Joined: Mon Oct 10, 2005 1:07 pm
Posts: 13
Hi,

I have a class (CategoryMap below) with many-to-one mapping to another class (CategoryMapType).

I execute the following HQL:

Code:
from CategoryMap as map
join map.type
join fetch map.sourceCategory as source
join fetch map.targetCategory as target
where map.type=?


and I get the follwing SQLs executed:
Code:
Hibernate: /*  from CategoryMap as map  join map.type join fetch map.sourceCategory as source join fetch map.targetCategory as target where map.type=? */
select categoryma0_.CATEGORY_MAP_ID as CATEGORY1_280_0_, categoryma1_.CATEGORY_MAP_TYPE_ID as CATEGORY1_281_1_, category2_.CATEGORY_ID as CATEGORY1_275_2_, category3_.CATEGORY_ID as CATEGORY1_275_3_, categoryma0_.UPDATE_TIMESTAMP as UPDATE2_280_0_, categoryma0_.START_DATE as START3_280_0_, categoryma0_.END_DATE as END4_280_0_, categoryma0_.CREATE_TIMESTAMP as CREATE5_280_0_, categoryma0_.UPDATE_USER_ID as UPDATE6_280_0_, categoryma0_.TYPE as TYPE280_0_, categoryma0_.SOURCE_CATEGORY_ID as SOURCE8_280_0_, categoryma0_.TARGET_CATEGORY_ID as TARGET9_280_0_, categoryma1_.UPDATE_TIMESTAMP as UPDATE2_281_1_, categoryma1_.CREATE_TIMESTAMP as CREATE3_281_1_, categoryma1_.UPDATE_USER_ID as UPDATE4_281_1_, categoryma1_.DESCRIPTION as DESCRIPT5_281_1_, categoryma1_.CODE as CODE281_1_, category2_.UPDATE_TIMESTAMP as UPDATE3_275_2_, category2_.CREATE_TIMESTAMP as CREATE4_275_2_, category2_.UPDATE_USER_ID as UPDATE5_275_2_, category2_.DESCRIPTION as DESCRIPT6_275_2_, category2_.TYPE as TYPE275_2_, category2_.SCOPE as SCOPE275_2_, category2_.CODE as CODE275_2_, category3_.UPDATE_TIMESTAMP as UPDATE3_275_3_, category3_.CREATE_TIMESTAMP as CREATE4_275_3_, category3_.UPDATE_USER_ID as UPDATE5_275_3_, category3_.DESCRIPTION as DESCRIPT6_275_3_, category3_.TYPE as TYPE275_3_, category3_.SCOPE as SCOPE275_3_, category3_.CODE as CODE275_3_
from CATEGORY_MAP categoryma0_
inner join CATEGORY_MAP_TYPE categoryma1_ on categoryma0_.TYPE=categoryma1_.CODE
inner join CATEGORY category2_ on categoryma0_.SOURCE_CATEGORY_ID=category2_.CATEGORY_ID
inner join CATEGORY category3_ on categoryma0_.TARGET_CATEGORY_ID=category3_.CATEGORY_ID
where categoryma0_.TYPE=?

Hibernate: /* load com...CategoryMapType */ select categoryma0_.CATEGORY_MAP_TYPE_ID as CATEGORY1_281_0_, categoryma0_.UPDATE_TIMESTAMP as UPDATE2_281_0_, categoryma0_.CREATE_TIMESTAMP as CREATE3_281_0_, categoryma0_.UPDATE_USER_ID as UPDATE4_281_0_, categoryma0_.DESCRIPTION as DESCRIPT5_281_0_, categoryma0_.CODE as CODE281_0_ from CATEGORY_MAP_TYPE categoryma0_ where categoryma0_.CODE=?


The second SQL repeats itself for every row retrieved by the first SQL. Since I eagerly fetch CategoryMapType in the first select, I don't see why the other selects are required! Note that there is no redundant select executed for the other many-to-one mapping (sourceCategory and TargetCategory). Could the problem be the fact that I use property-ref in CategoryMapType?

also see http://forum.hibernate.org/viewtopic.php?t=935481&highlight=propertyref+redundant+joins

Any help is appreciated.

Hibernate version: 3.1

Mapping documents:
Code:
<hibernate-mapping>
   <class name="com...CategoryMap" table="CATEGORY_MAP">

      <id name="id" column="CATEGORY_MAP_ID" type="long" unsaved-value="any">
         <generator class="seqhilo">
            <param name="sequence">CATEGORY_MAP_SEQ</param>
            <param name="max_lo">1000</param>
         </generator>
       </id>

      <timestamp column="UPDATE_TIMESTAMP" name="updateTimestamp"/>
      
       <property name="startDate"       column="START_DATE"        type="date"/>
      <property name="endDate"         column="END_DATE"          type="date"/>
      <property name="createTimestamp" column="CREATE_TIMESTAMP"  type="timestamp"/>
      <property name="updateUserId"    column="UPDATE_USER_ID"    type="string"/>

      <many-to-one name="type"
         column="TYPE"
         class="com...CategoryMapType"
         property-ref="key.id"
         cascade="save-update"
         lazy="proxy"
         fetch="join"/>

      <many-to-one name="sourceCategory"
         column="SOURCE_CATEGORY_ID"
         class="com...Category"
         cascade="save-update"
         fetch="join"
         lazy="proxy"/>

      <many-to-one name="targetCategory"
         column="TARGET_CATEGORY_ID"
         class="com...Category"
         cascade="save-update"
         fetch="join"
         lazy="proxy"/>

  </class>

</hibernate-mapping>


Code:
<hibernate-mapping>
   
   <class name="com...CategoryMapType"
      proxy="com...ICategoryMapType"
      table="CATEGORY_MAP_TYPE" >
      
      <id name="id" column="CATEGORY_MAP_TYPE_ID" type="long"   unsaved-value="any">
         <generator class="seqhilo">
            <param name="sequence">CATEGORY_MAP_TYPE_SEQ</param>
            <param name="max_lo">100</param>
         </generator>
      </id>
      
      <timestamp name="updateTimestamp" column="UPDATE_TIMESTAMP"/>
      <property  name="createTimestamp" column="CREATE_TIMESTAMP" type="timestamp"/>
      <property  name="updateUserId"    column="UPDATE_USER_ID"   type="string"/>
      <property  name="description"     column="DESCRIPTION"      type="string"/>
      
      <component name="key" class="com...ICategoryMapType$CategoryMapTypeKey">
         <parent name="entity"/>      
         <property name="id" column="CODE" type="string"   not-null="true"/>
       </component>
       
  </class>

   <class name="com...ICategoryMapType$CategoryMapTypeKey" table="CATEGORY_MAP_TYPE" >
      
      <id name="id" column="CODE" type="string" />
      
      <many-to-one name="entity"
         column="CATEGORY_MAP_TYPE_ID"
          class="com...CategoryMapType"
          fetch="join"/>
  </class>
   
</hibernate-mapping>


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 7:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you make the map.type join into a join fetch, do the extra selects go away? You won't see any extra joins in your initial SQL statement, so there's no harm in doing that, and if it disambiguates something for Hibernate, then it's probably a good thing.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 19, 2006 2:11 am 
Newbie

Joined: Mon Oct 10, 2005 1:07 pm
Posts: 13
no, it does not help, I still get the redundant selects.

the funny thing is that although I am doing the query using "where map.type = ?", I get the select for CategoryMapType once per row in the result. This mean Hibernate actually executed the exact same SQL more than once in the same HQL execution. This doesn't make sense to me.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 10:35 am 
Newbie

Joined: Sun Mar 26, 2006 10:23 am
Posts: 1
i have the same problem using property-ref in an eager fetching query. have you found a solution or workaround yet?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 10:40 am 
Newbie

Joined: Mon Oct 10, 2005 1:07 pm
Posts: 13
no, I still have the problem... it's not a showstopper (yet) so I am waiting for some response...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 3:54 am 
Newbie

Joined: Tue Jul 31, 2007 5:16 am
Posts: 9
Is there any solution now?
I have the same problem:

http://forum.hibernate.org/viewtopic.php?t=978077&highlight=


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 5:24 pm 
Newbie

Joined: Tue Mar 03, 2009 12:50 pm
Posts: 5
Location: New York
Has the original question of this post been answered? Did it have to do with the property-ref relationship?

3.2.6ga

I believe I am seeing the same issue. In my case, I have a query that looks something like this (I've tried to simplify these so they can be more readable):
Code:
Criteria criteria = getCriteria(ClassA.class)
             .setFetchMode("subOfB", FetchMode.JOIN)


where
subOfB is of class SubOfB
which is subclass of class SuperB

SubOfSubOfB is a subclass of SubOfB, and the type of the actual JOINED instances returned in the query above.

The B heirarchy is mapped with table-by-subclass strategy.

ClassA has a @OneToOne mapping to SubOfB, e.g. a part of SubOfB would look something like this:

Code:
@OneToOne(fetch = FetchType.LAZY, mappedBy = "subOfB")
protected ClassA classA;


The ClassA side looks something like this....

Code:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn
@ForeignKey(name = "FK_CLASSA_SUBOFB")
private SubOfB subOfB;


I'm running into the same issue as the email that started this thread. That is, when I call

Code:
Hibernate.initialize(criteria.list());


A single query with all the appropriate joins get created. Something like (along with a 2nd query)...

Code:
select * from ClassA, SubOfB, SuperB, SubOfSubOfB where (proper left join statements starting with ClassA)

select * from ClassA where subOfB_id=?


The 2nd query is run as many times as there are rows returned in the 1st query... even though all the contents appear redundant, since it's just a subset of data returned in the 1st query.

( By the way, if you change the fetch clauses above to FetchType.EAGER in the @OneToOne mappings above it makes no difference. Notice Criteria overrides fetch type. The fetch strategy hint does appear to work, since the 1st query is generated with all the necessary joins. Also, I made sure to set max_fetch_depth sufficiently high for testing purposes.)

With in B class heirarchy, primary keys appear to be mapped correctly. The resulting 1st query properly left joins on the subject, ClassA. So I'm confused as to why Hibernate would need to rerun queries for ClassA again based on subOfB_ids.


Top
 Profile  
 
 Post subject: workaround?
PostPosted: Mon Mar 09, 2009 4:07 pm 
Newbie

Joined: Tue Mar 03, 2009 12:50 pm
Posts: 5
Location: New York
Hi, I just wrote a reply to my own posting on this issue here:
http://forum.hibernate.org/viewtopic.php?p=2408307

If you happen to have had the same issue I was experiencing, I hope this helps you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.