以编程方式指定休眠中多对一的延迟加载
我正在使用 Hibernate 3.6 和 Spring 3.0.5。
我有以下用户对象的映射
<class name="foo.User" table="FOO_USER">
<id column="USER_ID" name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="firstName" column="FIRST_NAME" type="java.lang.String" length="100"/>
...
<many-to-one name="organization" column="ORGANIZATION_ID class="foo.Organization" not-null="true" update="false" />
...
用户与组织具有多对一关系。通常,我希望立即加载该关系,因此映射坚持默认设置lazy=false(不指定任何内容)。
在某些情况下,我不想急切地加载组织。我尝试用 Criteria 指定这一点
(User)getSession().createCriteria(User.class)
.add(Restrictions.eq("id",id))
.setFetchMode("organization", FetchMode.SELECT)
.uniqueResult();
,但提取模式被忽略。 Hibernate 仍然急切地加载组织关系。我已经为此绞尽脑汁几个小时了。任何帮助将不胜感激。
I am using Hibernate 3.6 with Spring 3.0.5.
I have the following mapping for a User object
<class name="foo.User" table="FOO_USER">
<id column="USER_ID" name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="firstName" column="FIRST_NAME" type="java.lang.String" length="100"/>
...
<many-to-one name="organization" column="ORGANIZATION_ID class="foo.Organization" not-null="true" update="false" />
...
The User has a many-to-one relationship with Organization. Usually, I want that relationship to be eagerly loaded, so the mapping sticks with the default setting of lazy=false (by not specifying anything).
There is a certain case where I do not want to eagerly load the Organization. I tried specifying this with a Criteria
(User)getSession().createCriteria(User.class)
.add(Restrictions.eq("id",id))
.setFetchMode("organization", FetchMode.SELECT)
.uniqueResult();
But the fetch mode is being ignored. Hibernate still eagerly loads the Organization relationship. I've been knocking my head against this for a few hours. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好将关联映射为惰性关联并使用获取策略来调整性能。我不相信有一种方法可以将某些东西映射为惰性,然后在特定实例中使其变得非惰性。当然 fetch=select 不会这样做,因为这并不意味着懒惰。请参阅“提高性能”的第 21.1 节 在参考手册中了解概念的解释。
It's always best to leave associations mapped as lazy and use fetching strategies to tune performance. I don't believe there's a way to map something as lazy and then make it un-lazy in a particular instance. Certainly fetch=select won't do it, as that doesn't imply anything about laziness. See section 21.1 of "Improving Performance" in the reference manual for an explanation of the concepts.
FetchType 可以使用 fetchProfiles 以编程方式设置,
例如:假设有一个 Employee 类,它有两个子类 Address 和 Department Fetch 配置文件,这些可以在 Employee 类中设置。
当您检索员工记录时,您可以启用 fetchprofile。如果启用了获取配置文件,则将急切地获取子对象,因为您正在执行联接
FetchType can be set programatically using fetchProfiles
e.g.: Consider there is an Employee class and it has two child classes Address and Department Fetch profiles for these can be set in the Employee class.
You can enable the fetchprofile When you are retrieve Employee record. If the fetch profile is enable then the child objects are eagerly fetch because you are doing a join