简体   繁体   English

休眠-一对一的n + 1个选择查询

[英]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: 该查询为每个组织+ 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. http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html告诉fetch="join"但这没有任何区别。 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. 由于您使用的是HQL来获取您的东西,因此简单地使用注释或尝试使用的属性来避免n + 1问题将无济于事。

The right solution would be to make use of 'FETCH JOIN' clause in your query. 正确的解决方案是在查询中使用“ FETCH JOIN”子句。 You can follow the following link for more details: http://www.realsolve.co.uk/site/tech/hib-tip-pitfall.php?name=n1selects 您可以单击以下链接获取更多详细信息: http : //www.realsolve.co.uk/site/tech/hib-tip-pitfall.php?name=n1selects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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