这就是问题
我有一个与我的班级Reseau有@ManytoOne
关系的java类Agence。
有我的Agence代码:
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>
答案 0 :(得分:2)
在你的reseau课程中,agences列表永远不会被初始化,它应该是:
@Entity
public class Reseau implements Serializable{
private int idReseau;
private String Libelle;
private List<Agence> Agences = new ArrayList<Agence>();
你的field,getter和setter声明都应该使用List类型,而不是Collection not ArrayList,你的字段不应该以大写字母开头(java命名约定):
private List<Agence> agences = new ArrayList<Agence>();
public List<Agence> getAgences() {
return agences;
}
public void setAgences(List<Agence> agences) {
this.agences = agences;
}
答案 1 :(得分:0)
问题是当您执行r.addAgence(a);
时,Agences
的列表可能对于少数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);
}
答案 2 :(得分:0)
由于您编辑了问题,我会将其作为答案发布。
根据此回复https://stackoverflow.com/a/2442036/1094430,如果您将数据库设置为自动生成,则不应按代码设置ID。
删除
r.setIdReseau(1);
...
a.setIdAgence(2);
...
a.setIdAgence(1990);
从您的代码中继续执行。
答案 3 :(得分: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();
}
}
它应该有效,因为两个对象都是链接的。