简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM