gpt4 book ai didi

java - hibernate 急切地加载通常是惰性的关联

转载 作者:行者123 更新时间:2023-12-02 00:50:12 24 4
gpt4 key购买 nike

我有这两个映射:

<hibernate-mapping>
<class name="sample.Operator" table="OPERATOR">
<id name="id" >
<generator class="native" />
</id>
<property name="name" not-null="true">
<column name="NAME" />
</property>
<set name="pointCodes" inverse="false" lazy="true" cascade="save-update">
<key>
<column name="OperatorID" />
</key>
<one-to-many class="sample.PointCode" />
</set>
</class>

<hibernate-mapping>
<class name="sample.PointCode" table="POINTCODE">
<id name="id">
<generator class="native" />
</id>
<properties name="pointCodeKey" unique="true">
<property name="pointCode" not-null="true">
</property>
<property name="networkIndicator" not-null="true">
</property>
</properties>
<property name="name" not-null="true">
</property>
</class>
</hibernate-mapping>

大多数情况下,在获取运算符(operator)时,我希望延迟获取 pointCode,因此我不想在映射中设置lazy="false",

但是我有一个查询,例如session.createQuery("from Operator").list() 我确实希望不延迟获取 pointCodes 关联 - 我该怎么做?

最佳答案

Hibernate 引用手册 writes :

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections. See Section 20.1, “Fetching strategies” for more information.

from Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens

A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). The associated objects are also not returned directly in the query results. Instead, they may be accessed via the parent object. The only reason you might need an alias is if you are recursively join fetching a further collection:

from Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens child
left join fetch child.kittens

The fetch construct cannot be used in queries called using iterate() (though scroll() can be used). Fetch should be used together with setMaxResults() or setFirstResult(), as these operations are based on the result rows which usually contain duplicates for eager collection fetching, hence, the number of rows is not what you would expect. Fetch should also not be used together with impromptu with condition. It is possible to create a cartesian product by join fetching more than one collection in a query, so take care in this case. Join fetching multiple collection roles can produce unexpected results for bag mappings, so user discretion is advised when formulating queries in this case. Finally, note that full join fetch and right join fetch are not meaningful.

关于java - hibernate 急切地加载通常是惰性的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3537162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com