-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate fetch profiles with many to many relationships
PostPosted: Tue Dec 13, 2016 6:22 am 
Newbie

Joined: Tue Dec 13, 2016 5:37 am
Posts: 1
Hi all,

I am new to Hibernate and I have just started using it in an application. I was reading about all of the different fetching strategies that it provides and I'm trying to play around with the Fetch Profiles feature so I ran some tests on my application:

- Test 1: default fetching strategy (which to my understanding is LAZY with SELECT as fetch mode)
- Test 2: activating EAGER fetch for the associations that I know will be used in my application (with JOIN as fetch mode)
- Test 3: creating Fetch Profiles for the same associations and enabling them programmatically with session.enableFetchProfile() at the beginning of the application (with JOIN as fetch mode as well)

To my surprise, Test 3 ran slower than Test 2 although the same associations are being eagerly fetched in both cases. I investigated the issue further and found a difference between the SQL queries generated by these tests. The problem seems to stem from a many-to-many association that I am eagerly fetching (which translates into a bridge table in the SQL database).

When using eager fetch, the association is eagerly fetched as any other (through joining with the bridge table) and all subsequent associations are also fetched. However, when using fetch profiles, this many-to-many association is NOT fetched (i.e. the generated SQL query does not have a JOIN clause with the bridge table) although the corresponding fetch profile is correctly activated.

Is this a problem with the way I am using fetch profiles? Am I missing some other option that I need to activate? Or is it a limitation of the fetch profile feature?

Here's the section of the XML mapping file that corresponds to the many-to-many relationship (obviously, I set the lazy property on the "unsharedParts" set to "true" when using fetch profiles):

Code:
<class name="Assembly">
   ...
   <subclass name="BaseAssembly" discriminator-value="base">
      <set name="unsharedParts" table="assemblyToPart" lazy="false" fetch="join">
         <key>
            <column name="baseAssemblyId" index="assemblyToPartAssmIndex"/>
         </key>
         <many-to-many column="compositePartId" class="CompositePart" />
      </set>
   </subclass> 
</class>

And here's my Hibernate configuration file:
Code:
<hibernate-configuration>
    <session-factory>
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
     <property name="hibernate.connection.url">...</property>
     <property name="hibernate.connection.username">...</property>
     <property name="hibernate.connection.password">...</property>
    
     <property name="hibernate.current_session_context_class">thread</property>
     <!-- <property name="hibernate.max_fetch_depth">10</property>
     <property name="hibernate.default_batch_fetch_size">0</property>
     <property name="hibernate.batch_size">25</property> -->
     <property name="show_sql">false</property>
     <property name="hibernate.generate_statistics">true</property>
    
     <mapping resource="..." ></mapping>
    </session-factory>

</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: Hibernate fetch profiles with many to many relationships
PostPosted: Wed Dec 14, 2016 7:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If you have a replicating test case, then you should open a new Jira ssue.

Meanwhile, use JPQL queries with JOIN FETCH as a workaround.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.