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

时间:2017-06-24 13:23:34

标签: java hibernate javafx

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

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

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

要点:

=&GT;在FXMLAddValueController的e.getDdd()。add(d)行发生异常,但这行已经在hibernate session的范围内了

=&GT;我使用了fetch = FetchType.Lazy,因为我不想要子数据

=&GT;我想要特定的解决方案以及发生这种情况的原因。

=&GT;它是一个桌面应用,所以我只有hibernate.cfg.xml。

=&GT;我正在使用一对多映射

1 个答案:

答案 0 :(得分:0)

这是因为getEmployee的事务(p_id);已经关闭。查询应该在同一个交易中。