-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: detached entity passed to persist - Whats wrong?
PostPosted: Thu May 03, 2007 11:52 am 
Beginner
Beginner

Joined: Sun Sep 04, 2005 3:10 am
Posts: 24
Location: CH
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 12:14 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I only have a passing knowledge of Annotations, but I do know that persist() only takes new objects. I assume the constructor new PMQ(new Long(args[0]), args[1]); is setting the primary key, indicating to Hibernate that it is a detached instance instead of a transient instance, since the primary key is marked as @GeneratedValue.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 3:14 pm 
Beginner
Beginner

Joined: Sun Sep 04, 2005 3:10 am
Posts: 24
Location: CH
Ananasi,
you made my day!
Thanks, chk


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.