emmma1223
emmma1223

Reputation: 67

Hibernate - n+1 select queries with 1-to-1

Organization is mapped to Address as 1-to-1:

Organization:

<one-to-one class="Address" constrained="true" name="address" property-ref="organizationId"/>

Address:

<many-to-one class="Organization"  name="organization">
          <column name="OrganizationID" not-null="false" unique="true"/>
  </many-to-one>

this query generates addtitional select for every Organization + 1:

   query = session.createQuery("select o from Organization as o where o.isCool=0").setReadOnly(true);
   organizations = query.list();

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html tells to fetch="join" but this doesn't make any difference. How to solve this problem? Any help is appreciated.

EDIT In debugger i can see that address is actually not lazy loaded, i have no idea why.

Upvotes: 0

Views: 1173

Answers (1)

PaiS
PaiS

Reputation: 1282

Since you are using an HQL to fetch your stuff, it would not help to simply use the annotation or the attribute that you are trying, to avoid the n+1 problem.

The right solution would be to make use of 'FETCH JOIN' clause in your query. You can follow the following link for more details: http://www.realsolve.co.uk/site/tech/hib-tip-pitfall.php?name=n1selects

Upvotes: 1

Related Questions