這裏的問題是
不能堅持我的對象
我有一個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>
請發佈堆棧跟蹤 – PSR
現在我做了,謝謝 – NumberTen
請發佈persistence.xml,我相信那裏存在問題。我認爲沒有命名爲「測試」的持久性單元正確定義 –