2013-05-17 276 views
0

這裏的問題是
不能堅持我的對象

我有一個Java類法新社說有我的課送貨線路一@ManytoOne關係。

有我法新社代碼:

package ma.kafil.bank; 

import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.ManyToOne; 


@Entity 
public class Agence implements Serializable{ 

    private int idAgence; 
    private String labelle; 
    private Reseau reseau; 

    public Agence(String labelle) { 
    this.labelle = labelle; 
    } 



    public Agence() { 
    } 

    @ManyToOne 
    public Reseau getReseau() { 
    return reseau; 
    } 

    public void setReseau(Reseau reseau) { 
    this.reseau = reseau; 
    } 



    @Id 
    @GeneratedValue 
    public int getIdAgence() { 
    return idAgence; 
    } 

    public void setIdAgence(int idAgence) { 
    this.idAgence = idAgence; 
    } 

    public String getLabelle() { 
    return labelle; 
    } 

    public void setLabelle(String labelle) { 
    this.labelle = labelle; 
    } 



} 

和我的送貨線路等級:

package ma.kafil.bank; 

    import java.io.Serializable; 
    import java.util.ArrayList; 
    import java.util.Collection; 
    import javax.persistence.CascadeType; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.Id; 
    import javax.persistence.OneToMany; 


    @Entity 
    public class Reseau implements Serializable{ 
     private int idReseau; 
     private String Libelle; 
     private ArrayList<Agence> Agences = new ArrayList(); 

     //Constructor without arguments 
     public Reseau() { 
     } 

     public Reseau(String Libelle) { 
     this.Libelle = Libelle; 
     } 

     public void ajouterAgence(Agence a){ 
     Agences.add(a); 
     } 


     @Id 
     @GeneratedValue 
     public int getIdReseau() { 
     return idReseau; 
     } 

     public void setIdReseau(int idReseau) { 
     this.idReseau = idReseau; 
     } 

     public String getLibelle() { 
     return Libelle; 
     } 

     public void setLibelle(String Libelle) { 
     this.Libelle = Libelle; 
     } 


     @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="reseau") 
     public Collection<Agence> getAgences() { 
     return Agences; 
     } 



     public void setAgences(ArrayList<Agence> Agences) { 
     this.Agences = Agences; 
     } 

     public void addAgence(Agence a){ 
     this.Agences.add(a); 
     } 





    } 

最後的主類:

package ma.kafil.bank; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 


public class Main { 
    public static void main(String [] args){ 
    //Creating Entity Manager 
    //persistence-unit name="test" 

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("test"); 
    EntityManager em = emf.createEntityManager(); 

    //beginning the transaction 
    em.getTransaction().begin(); 


    //Creating a new Reseau r with r.labbelle = "Centre" and r.id=1 
    Reseau r = new Reseau("Centre"); 
    r.setIdReseau(1); 

    //Creating a new Agence a 
    Agence a = new Agence("Nom Agence"); 
    a.setIdAgence(2); 

    //adding a to my Reseau (What's the problem with that ??) 
    r.addAgence(a); 
    em.persist(r); 

    a.setIdAgence(1990); 
    a.setReseau(r); 

    em.persist(a); 
    em.getTransaction().commit(); 

    //closing em and emf 
    em.close(); 
    emf.close(); 



    } 

} 

這裏是堆棧跟蹤:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: ma.kafil.bank.Reseau 
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226) 
    at ma.kafil.bank.Main.main(Main.java:30) 
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: ma.kafil.bank.Reseau 
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79) 
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38) 
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618) 
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592) 
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220) 
    ... 1 more 
Java Result: 1 

而這裏的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="test" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class> ma.kafil.bank.Agence</class> 
    <class> ma.kafil.bank.Reseau</class> 

    <properties> 
     <property name="hibernate.connection.username" value="root"/> 
     <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
     <property name="hibernate.connection.password" value=""/> 
     <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/awbawards"/> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
    </properties> 
     </persistence-unit> </persistence> 

由於提前

+1

請發佈堆棧跟蹤 – PSR

+0

現在我做了,謝謝 – NumberTen

+0

請發佈persistence.xml,我相信那裏存在問題。我認爲沒有命名爲「測試」的持久性單元正確定義 –

回答

2

在你的網格類中,AGENCES列表永遠不會初始化時,它應該是:

@Entity 
    public class Reseau implements Serializable{ 
     private int idReseau; 
     private String Libelle; 
     private List<Agence> Agences = new ArrayList<Agence>(); 

而且你的領域,getter和setter聲明都應該使用類型List,而不是Collection而不是ArrayList,並且您的字段不應以大寫字母(java命名約定)開頭:

private List<Agence> agences = new ArrayList<Agence>(); 

    public List<Agence> getAgences() { 
     return agences; 
    } 

    public void setAgences(List<Agence> agences) { 
     this.agences = agences; 
    } 
+0

但是後來我得到這個: ' 線程「main」中的異常javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:傳遞給persist:ma.kafil.bank的分離實體。網格 \t在org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637) \t在org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226) \t在ma.kafil.bank.Main。 main(Main.java:30)' – NumberTen

+0

這是另一個問題,但他的解決方案就是你需要通過你現在得到的'NPE'。 嘗試刪除任何'a.setIdAgence(...);'行,看它是否會給你新的異常 – StepTNT

+0

爲什麼我應該使用'ArrayList'而不是集合和'ArrayList'? – NumberTen

0

問題是當你做r.addAgence(a);有可能Agences列表爲空Reseau爲空。快速修復可以在您的添加方法中完成 -

public void addAgence(Agence a){ 
    if(Agences==null){//do this check to avoid null pointer 
     Agences = new ArrayList<Agence>(); 
    } 
    this.Agences.add(a); 
} 
+0

我仍然有這個問題:傳遞給persist的分離實體:ma.kafil.bank.Reseau – NumberTen

+0

這是一個不同的問題,它與您先前的空指針異常無關。 – Kshitij

+0

請參閱此鏈接的分離實體持久性問題 - http://stackoverflow.com/questions/6378526/org-hibernate-persistentobjectexception-detached-entity-passed-to-persist – Kshitij

0

由於您編輯了您的問題,因此我會將其作爲回答發佈。

根據此回覆https://stackoverflow.com/a/2442036/1094430如果DB設置爲自動生成,則不應通過代碼設置ID。

所以刪除

r.setIdReseau(1); 
... 
a.setIdAgence(2); 
... 
a.setIdAgence(1990); 
從您的代碼

,然後進行持續。

+0

'線程中的異常「主」javax.persistence.PersistenceException :'org.hibernate.PropertyAccessException:拋出:IllegalArgumentException同時呼籲在org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)ma.kafil.bank.Reseau.agences \t的二傳手發生 \t在org.hibernate.ejb .AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226) \t at ma.kafil.bank.Main.main(Main.java:29) – NumberTen

+0

@ Kafil10你的getter和setter不正確,看我更新的答案 – Guillaume

+0

謝謝你@Guillaume – NumberTen

0

嘗試用這個主:

package ma.kafil.bank; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 


public class Main { 
    public static void main(String [] args){ 
    //Creating Entity Manager 
    //persistence-unit name="test" 

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("test"); 
    EntityManager em = emf.createEntityManager(); 

    //beginning the transaction 
    em.getTransaction().begin(); 


    //Creating a new Reseau r with r.labbelle = "Centre" and r.id=1 
    Reseau r = new Reseau("Centre"); 
    r.setIdReseau(1); 

    //Creating a new Agence a 
    Agence a = new Agence("Nom Agence"); 
    a.setIdAgence(2); 
    a.setIdAgence(1990); 
    a.setReseau(r);  

    //adding a to my Reseau (What's the problem with that ??) 
    r.addAgence(a); 
    em.persist(r); 

    em.getTransaction().commit(); 

    //closing em and emf 
    em.close(); 
    emf.close(); 



    } 

} 

它應該工作,因爲這兩個對象鏈接。