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>