org.hibernate.LazyInitializationException:已经尝试了所有可能的解决方案:(



在发生此异常的行,它已经在会话范围中,即会话可用于代理类,但是发生了此例外。

发生错误的控制器类代码:

  try{
    SessionFactory sessionfactory = MyFactory.getSessionFactory();
              Session s = sessionfactory.openSession();
    EmployeeDetails e = getEmployee(p_id);
        DailyDataDetails d = new DailyDataDetails();
            d.setEmp_id(p_id);
            d.setEmp_name(p_fname+" "+p_lname);
            d.setPayable(pay);
            d.setRate(Float.parseFloat(txtrate.getText()));
            d.setSpindles(Integer.parseInt(txtSpindle.getText()));
            d.setHour(hr);
            d.setMin(min);
            d.setWork_date( java.sql.Date.valueOf(select_date.getValue()));

                 s.beginTransaction();
                 e.getDdd().add(d);
            s.save(e);
            s.getTransaction().commit();
            s.close();
             Notifications notiBuild = Notifications.create()
                        .title("Value Added")
                        .text("Value added sucessfully")
                        .graphic(node)
                        .hideAfter(Duration.seconds(5))
                        .darkStyle()
                        .position(Pos.TOP_CENTER);
                notiBuild.darkStyle();
                notiBuild.show();
                loadData();

    }catch(Exception e){ e.printStackTrace();}

例外:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: models.EmployeeDetails.ddd, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:392)
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:297)
at empman.FXMLAddValueController.toAddData(FXMLAddValueController.java:174)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这是我的模型类:

@OneToMany(fetch = FetchType.LAZY)
@JoinTable(joinColumns=@JoinColumn(name="eid"),inverseJoinColumns=@JoinColumn(name="id"))
private Collection<DailyDataDetails> ddd = new ArrayList<DailyDataDetails>();
public int getEid() {
    return eid;
}
public void setEid(int eid) {
    this.eid = eid;
}
public Collection<DailyDataDetails> getDdd() {
    return ddd;
}
public void setDdd(Collection<DailyDataDetails> ddd) {
    this.ddd = ddd;
}

注意:

=>异常出现在e.getddddddddddddddd((。add(d(中的fxmladdvaluecontroller中,但是此行已经在Hibernate Session

的范围中

=>我已经使用了fetch = fetchype.lazy,因为我不想要子数据

=>我想要特定的解决方案和这种情况的原因。

=>这是一个桌面应用,所以我只有Hibernate.cfg.xml。

=>我正在使用一对多映射

这是因为getEmployee(p_id(的交易;已经关闭了。查询应在同一交易中。

相关内容

最新更新