未能懒散地初始化角色集合 [英] failed to lazily initialize a collection of role

查看:124
本文介绍了未能懒散地初始化角色集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Indicator implements Serializable {
...

@OneToMany(mappedBy =indicator,fetch = FetchType.LAZY)
private List< IndicatorAlternateLabel> indicatorAlternateLabels;

公共列表< IndicatorAlternateLabel> getIndicatorAlternateLabels(){
return indicatorAlternateLabels;
}

public void setIndicatorAlternateLabels(List< IndicatorAlternateLabel> indicatorAlternateLabels){
this.indicatorAlternateLabels = indicatorAlternateLabels;
}
...
}

public class IndicatorAlternateLabel实现Serializable {
...
@ManyToOne(cascade = CascadeType.REFRESH ,fetch = FetchType.EAGER)
@JoinColumn(name =IndicatorID)
@XmlTransient
private指标指标;
...
}

当我像这样使用它们时:

  public MetricTypeDetail getMetricTypeDetail(Integer metricTypeId){
Criteria crit = sessionFactory.getCurrentSession()。createCriteria(Indicator.class, sub)
.add(Restrictions.eq(number,metricTypeId))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true);
crit.setMaxResults(1);
指标指标=(指标)crit.uniqueResult();
MetricTypeDetail metricTypeDetail = new MetricTypeDetail(indicator);
列表< IndicatorAlternateLabel> indicatorAlternateLabels = null;
indicatorAlternateLabels = indicator.getIndicatorAlternateLabels();
metricTypeDetail.setIndicatorAlternateLabels(indicatorAlternateLabels);
返回metricTypeDetail;
}

这段代码返回一个异常:
未能延迟初始化一个集合角色:com.porism.service.domain.Indicator.indicatorAlternateLabels,没有会话或会话已关闭



任何想法?我对Hibernate非常陌生

当您获取一个通常包含一个延迟加载的集合的对象时,就会发生延迟异常,并且尝试访问该集合。



您可以通过访问事务内的懒惰集合来避免此问题。



  • 使用 Hibernate.initialize(obj);
  • 获取集合
  • 另一项交易

  • 使用 获取配置文件 选择延迟/非延迟获取运行时

  • 将fetch设置为非懒惰(通常不会推荐)



此外,我建议您查看 相关的 链接到你的权利,这个问题已经被多次回答。另请参阅 Hibernate延迟加载应用程序设计


Hi I have two classes like this:

public class Indicator implements Serializable {
...

    @OneToMany(mappedBy = "indicator",fetch=FetchType.LAZY)
    private List<IndicatorAlternateLabel>  indicatorAlternateLabels;

    public List<IndicatorAlternateLabel> getIndicatorAlternateLabels() {
        return indicatorAlternateLabels;
    }

        public void setIndicatorAlternateLabels(List<IndicatorAlternateLabel> indicatorAlternateLabels) {
            this.indicatorAlternateLabels = indicatorAlternateLabels;
        }
...
    }

public class IndicatorAlternateLabel implements Serializable {
...
 @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinColumn(name = "IndicatorID")
    @XmlTransient
    private Indicator indicator;
...
}

When I use them like this:

 public MetricTypeDetail getMetricTypeDetail(Integer metricTypeId) {
        Criteria crit = sessionFactory.getCurrentSession().createCriteria(Indicator.class, "sub")
                    .add(Restrictions.eq("number", metricTypeId))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true);
        crit.setMaxResults(1);
        Indicator indicator=(Indicator) crit.uniqueResult();
        MetricTypeDetail metricTypeDetail=new MetricTypeDetail(indicator);
        List<IndicatorAlternateLabel> indicatorAlternateLabels = null;
        indicatorAlternateLabels=indicator.getIndicatorAlternateLabels();
        metricTypeDetail.setIndicatorAlternateLabels(indicatorAlternateLabels);
        return metricTypeDetail;
    }

This code returns an exception: failed to lazily initialize a collection of role: com.porism.service.domain.Indicator.indicatorAlternateLabels, no session or session was closed

Any idea? I'm very new to Hibernate

解决方案

Lazy exceptions occur when you fetch an object typically containing a collection which is lazily loaded, and try to access that collection.

You can avoid this problem by

  • accessing the lazy collection within a transaction.
  • Initalizing the collection using Hibernate.initialize(obj);
  • Fetch the collection in another transaction
  • Use Fetch profiles to select lazy/non-lazy fetching runtime
  • Set fetch to non-lazy (which is generally not recommended)

Further I would recommend looking at the related links to your right where this question has been answered many times before. Also see Hibernate lazy-load application design.

这篇关于未能懒散地初始化角色集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆