Hello,
is somebody able to explain that error to me. It is a mickymouse application and I am not able to get it right.
I always receive the error "detached entity passed to persist".
What does it mean? What have I done wrong?
Please help.
Thankyou, chk
Hibernate version:
hibernate annotations Version: 3.2.0.CR3
Product: Hibernate EntityManager Version: 3.2.0.CR3
Hibernate-Version: 3.2.0.cr5
Mapping documents:
Code:
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="PMQ" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ch.bfh.pmq.PMQ</class>
<class>ch.bfh.pmq.Message</class>
<properties>
<property name="hibernate.show_sql"
value="true"/>
<property name="hibernate.format_sql"
value="true"/>
<property name="use_sql_comments"
value="true"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost/bfh"/>
<property name="hibernate.connection.driver_class"
value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username"
value="root"/>
<property name="hibernate.connection.password"
value=""/>
<property name="show_sql"
value="true"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLMyISAMDialect"/>
<property name="dialect"
value="org.hibernate.dialect.MySQLMyISAMDialect"/>
<property name="hibernate.c3p0.min_size"
value="5"/>
<property name="hibernate.c3p0.max_size"
value="20"/>
<property name="hibernate.c3p0.timeout"
value="300"/>
<property name="hibernate.c3p0.max_statements"
value="50"/>
<property name="hibernate.c3p0.idle_test_period"
value="3000"/>
<!-- <property name="hibernate.max_fetch_depth" value="3"/> -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.isolation" value="8" /><!-- 1,2,4,8 -->
<property name="hibernate.connection.autocommit" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
</persistence>
Code:
public class PMQ implements Serializable
{
@Id
@GeneratedValue
@Column(name="QUEUE_ID")
protected long id;
@Column(name="QUEUE_NAME")
protected String qname;
@OneToMany (cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name="QUEUE_MESSAGE",
joinColumns={@JoinColumn(name="QUEUE_ID")},
inverseJoinColumns={@JoinColumn(name="MESSAGE_ID")})
protected List<Message> messages = new Vector<Message>();
public PMQ()
Code between sessionFactory.openSession() and session.close(): Code:
EntityManagerFactory");
emf = Persistence.createEntityManagerFactory("PMQ");
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
PMQ pmq = new PMQ(new Long(args[0]), args[1]);
PMQ storedPMQ = em.find(PMQ.class, new Long(args[0]));
if(storedPMQ != null){
em.merge(storedPMQ);
}else{
em.persist(pmq);
}
tx.commit();
Full stack trace of any exception that occurs:
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: ch.bfh.pmq.PMQ
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
at ch.bfh.PMQInit.main(PMQInit.java:76)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: ch.bfh.pmq.PMQ
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:212)
Name and version of the database you are using:
mysql 3.23.58