Hibernate:如何根据特定条件从另一个实体获取或加载惰性实体?
Hibernate: How to fetch or load a lazy entity from another entity on base of particular condition?
我想在下面的示例中加载 "getBulla",但是基于与 "getControl" 的比较。
@OneToMany(mappedBy = "bullabase", fetch=FetchType.LAZY)
public Set<BullaBase> getbulla() {
return this.bulla;
}
@Column(name = "Control", length = 40)
public String getControl() {
return Control; //values: Mahidana, Gana, Funa, Khana, Bona, Nona, Raka, Tuka etc.
}
getControl
包含 10 - 15 个不同的静态字符串,但应根据特定条件加载 Bulla,即加载所有实体并仅为 control == "mahidana"
获取 Bulla 实体,并且在单次获取中也是如此
不要认为有开箱即用的解决方案,但解决方法是一种环绕方法,它会在特定条件下在内部初始化集合:
public class CustomEntityRepo{
public Entity findById(Long id){
Entity entity = session.get(Entity.class, id);
if(entity.getControl.equals(\* custom condition*\)){
entity.getBulla.().size(); // init the collection
}
return entity;
}
}
我想在下面的示例中加载 "getBulla",但是基于与 "getControl" 的比较。
@OneToMany(mappedBy = "bullabase", fetch=FetchType.LAZY)
public Set<BullaBase> getbulla() {
return this.bulla;
}
@Column(name = "Control", length = 40)
public String getControl() {
return Control; //values: Mahidana, Gana, Funa, Khana, Bona, Nona, Raka, Tuka etc.
}
getControl
包含 10 - 15 个不同的静态字符串,但应根据特定条件加载 Bulla,即加载所有实体并仅为 control == "mahidana"
获取 Bulla 实体,并且在单次获取中也是如此
不要认为有开箱即用的解决方案,但解决方法是一种环绕方法,它会在特定条件下在内部初始化集合:
public class CustomEntityRepo{
public Entity findById(Long id){
Entity entity = session.get(Entity.class, id);
if(entity.getControl.equals(\* custom condition*\)){
entity.getBulla.().size(); // init the collection
}
return entity;
}
}