NumberTen
NumberTen

Reputation: 94

Can't persist my objects

here's is the problem

I have a java class Agence that has a @ManytoOne relationship with my class Reseau .

there is my Agence code :

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



}

and my Reseau Class :

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





    }

And Finally the main class :

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();



    }

}

Here is the stack trace :

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

And here is 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>

Thanks in Advance

Upvotes: 0

Views: 883

Answers (4)

Guillaume
Guillaume

Reputation: 14656

In your reseau class, the agences list is never initialized, it should be:

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

Also your field, getter and setter declarations should all use the type List, not Collection not ArrayList and your field shouldn't start with a capital letter (java naming conventions):

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

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

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

Upvotes: 2

cosmin.danisor
cosmin.danisor

Reputation: 963

Try with this main:

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();



    }

}

It should work, because both objects are linked.

Upvotes: 0

StepTNT
StepTNT

Reputation: 3967

Since you edited your question I'll post it as an answer.

Based on this reply https://stackoverflow.com/a/2442036/1094430 you should not set your ID by code if you DB is set to auto generate them.

So remove

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

from your code and then do the persist.

Upvotes: 0

Kshitij
Kshitij

Reputation: 8614

The problem is when you do r.addAgence(a); it is possible that list of Agences is null for few Reseau. A quick fix can be done in your add method -

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

Upvotes: 0

Related Questions