排除在Hibernate对象中加载特定属性 [英] Exclude Loading of specific properties in Hibernate object
问题描述
我有一个实体类
class B {
@ManyToOne(fetch = FetchType.EAGER)
私人A a;
@ManyToOne(fetch = FetchType.LAZY)
private C c;
}
在某些情况下,I不想加载对象A,因为我已经拥有该对象。
和C也一样。但是,这些都是我不想加载这些对象的特定场景。
有什么办法可以告诉hibernate不要加载实体对象的某些属性。 {没有渴望/取得建议。我只想在特定情况下发生这种情况]
注意:我正在使用Criteria来获取对象。
列表结果= sess.createCriteria(B.class)
.add(限制.like(name,yourcondition%))
.setFetchMode(a,FetchMode.EAGER)
.setFetchMode(c,FetchMode.LAZY)
.list( );
编辑:
由于FetchMode.EAGER和FetchMode.LAZY已被弃用,因此FetchMode.SELECT或FetchMode.JOIN
List结果= sess.createCriteria(B.class)
.add(name,yourcondition%))
.setFetchMode(a,FetchMode.JOIN)
.setFetchMode(c,FetchMode.SELECT)
.list();
I have a entity class
class B{
@ManyToOne(fetch = FetchType.EAGER)
private A a;
@ManyToOne(fetch = FetchType.LAZY)
private C c;
}
In certain scenarios, I don't want to load object A, since I already have that object. and same for C also. But these are specific scenarios I don't want to load those objects at all.
is there any way we can tell hibernate not to load certain properties of entity object. {no Eager/Fetch suggestions. I just want this to happen only in specific cases]
Note: I am using Criteria to fetch the object right now.
Since you're using HQL
for your queries, you can specify your fetching strategy at runtime based on your condition using the "fetch" keyword, like below:
List result = sess.createCriteria(B.class)
.add( Restrictions.like("name", "yourcondition%") )
.setFetchMode("a", FetchMode.EAGER)
.setFetchMode("c", FetchMode.LAZY)
.list();
Edit:
Since FetchMode.EAGER and FetchMode.LAZY is deprecated use, FetchMode.SELECT or FetchMode.JOIN
List result = sess.createCriteria(B.class)
.add( Restrictions.like("name", "yourcondition%") )
.setFetchMode("a", FetchMode.JOIN)
.setFetchMode("c", FetchMode.SELECT)
.list();
If you want to avoid the SELECT or JOIN check here.
这篇关于排除在Hibernate对象中加载特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!