2017-06-24 31 views
0

在在此異常發生的線,它已經在會話範圍內即會提供您的代理類又是發生此異常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([email protected](name="eid"),[email protected](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.getDdd()添加(d)在FXMLAddValueController發生

=>異常,但此行已經處於休眠會話的範圍

= >我使用了fetch = FetchType.Lazy,因爲我不想要子數據

=>我想要特定的解決方案以及發生這種情況的原因。

=>這是一個桌面應用程序,所以我只有hibernate.cfg.xml。

=>我使用1對多的映射

回答

0

這是因爲對於getEmployee的(P_ID)交易;已經關閉。查詢應該在同一個事務中。

相關問題