【问题标题】:Hibernate: How to fetch or load a lazy entity from another entity on base of particular condition?Hibernate:如何根据特定条件从另一个实体获取或加载惰性实体?
【发布时间】:2018-01-11 08:36:24
【问题描述】:

我想在以下示例中加载“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 实体,并且在单次提取中也是如此

【问题讨论】:

    标签: java hibernate jpa lazy-loading eager-loading


    【解决方案1】:

    不要认为有一个开箱即用的解决方案,但一种解决方法是一种环绕方法,它会在特定条件下在内部初始化集合:

    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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 2013-10-20
      • 2014-01-04
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多