0

这就是问题所在

我有一个 java 类 Agence ,它@ManytoOne与我的类 Reseau 有关系。

有我的代理代码:

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



}

和我的 Reseau 课程:

    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>

提前致谢

4

4 回答 4

2

在您的 reseau 课程中,代理列表永远不会初始化,它应该是:

    @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;
    }
于 2013-05-17T10:21:17.460 回答
0

问题是,当您这样做r.addAgence(a);时, list of 可能Agences为 null Reseau。可以在您的 add 方法中完成快速修复 -

public void addAgence(Agence a){
   if(Agences==null){//do this check to avoid null pointer
      Agences = new ArrayList<Agence>();
   }
   this.Agences.add(a);
}
于 2013-05-17T10:25:56.980 回答
0

既然你编辑了你的问题,我会把它作为答案发布。

根据此回复https://stackoverflow.com/a/2442036/1094430如果您的数据库设置为自动生成它们,则不应通过代码设置您的 ID。

所以删除

r.setIdReseau(1);
...
a.setIdAgence(2);
...
a.setIdAgence(1990);

从您的代码中,然后执行持久化。

于 2013-05-17T10:35:04.477 回答
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();



    }

}

它应该可以工作,因为两个对象都是链接的。

于 2013-05-17T10:57:32.680 回答