Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

java - failed to lazily initialize a collection of role

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...