4 Replies Latest reply on Jun 24, 2007 8:53 AM by Ignacio de Cordoba

    detached entity passed to persist

    Laszlo Graf Newbie

      Hi all,

      I have an entity, named AdatlapElozetesR, wich looks like this:

      package mypackage;
      
      import java.util.*;
      import javax.persistence.*;
      
      @Entity
      @Table(name = "ADATLAPELOZETESR", uniqueConstraints = {})
      public class AdatlapElozetesR implements java.io.Serializable {
      
       private static final long serialVersionUID = -7341044940631282534L;
       private Integer id;
       private Date datum;
      
       public AdatlapElozetesR() { }
      
       public AdatlapElozetesR(Integer id,
       Date datum) {
       this.id = id;
       this.datum = datum;
       }
      
       public AdatlapElozetesR(AdatlapElozetesR p_adatlapelozetesr) {
       this.id = p_adatlapelozetesr.getId();
       this.datum = p_adatlapelozetesr.getDatum();
       }
      
       @Id
       @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
       @SequenceGenerator(name="SQC_ALE", sequenceName="ADAT.SQ_ADATLAP_ELOZETES", allocationSize=1)
       @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQC_ALE")
       public Integer getId() { return this.id; }
       public void setId(Integer id) { this.id = id; }
      
       @Temporal(TemporalType.DATE)
       @Column(name = "datum", unique = false, nullable = false, insertable = true, updatable = true, length = 13)
       public Date getDatum() { return this.datum; }
       public void setDatum(Date datum) { this.datum = datum; }
      
      }
      


      To manage this entity I use a session bean:
      package mypackage.adat;
      
      import java.sql.*;
      import java.text.Format;
      import java.text.SimpleDateFormat;
      import java.util.*;
      import javax.ejb.Stateless;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      import javax.naming.InitialContext;
      import javax.sql.DataSource;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;
      import org.apache.log4j.*;
      import org.hibernate.*;
      import org.hibernate.cfg.*;
      
      import mypackage.AdatlapElozetesR;
      
      @Stateless
      public class AdatlapElozetesRBean implements AdatlapElozetesRRemote, AdatlapElozetesRLocal, java.io.Serializable {
      
       private static final Logger log = Logger.getLogger(AdatlapElozetesRBean.class);
       @PersistenceContext(unitName="persistUniter")
       private EntityManager em;
      
       public void persist(AdatlapElozetesR transientInstance) {
       //AdatlapElozetesR n = new AdatlapElozetesR(transientInstance);
       System.out.println((transientInstance == null ? "transientInstance NULL" :"transientInstance OK, "+transientInstance));
       try {
       em.persist(transientInstance);
       log.debug("persist successful");
       } catch (RuntimeException re) {
       log.error("persist failed", re);
       throw re;
       }
       }
      
      ...
      }
      

      And my test client looks like this:
      package mypackage.test;
      
      import java.util.*;
      import javax.naming.InitialContext;
      
      import org.hibernate.*;
      import org.hibernate.cfg.*;
      
      import mypackage.adat.*;
      import mypackage.*;
      
      public class AdatlapElozetesRTest {
      
       AdatlapElozetesRTest() {
       System.out.println("AdatlapElozetesRTest()");
       }
       private void CreateAdatlapElozetesR() {
       System.out.println("CreateAdatlapElozetesR()");
       AdatlapElozetesR ale = new AdatlapElozetesR(new Integer(0),
       new Date());
       System.out.println((ale == null ? "ale NULL" : "ale OK"));
       try {
       Properties properties = new Properties();
       properties.setProperty("java.naming.provider.url", "jnp://localhost:1099");
       properties.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
       properties.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
       InitialContext ctx = new InitialContext( properties );
       AdatlapElozetesRInterface aei = (AdatlapElozetesRInterface)ctx.lookup("AdatlapElozetesRBean/remote");
       System.out.println((aei == null ? "aei NULL" : "aei OK"));
       aei.persist(ale);
       } catch(org.hibernate.PersistentObjectException poex) {
       System.out.println("feliras()\n"+poex);
       //ex.printStackTrace();
       } catch(Exception ex) {
       System.out.println("feliras()\n"+ex);
       //ex.printStackTrace();
       }
       }
       public static void main(String[] args) {
       AdatlapElozetesRTest t1 = new AdatlapElozetesRTest();
       t1.CreateAdatlapElozetesR();
       t1 = null;
       }
      }
      

      When I run the client, to insert a new row in my table I get this error:
      21:47:37,032 INFO [STDOUT] transientInstance OK, mypackage.adat.AdatlapElozetes@80d11b
      21:47:37,032 ERROR [AdatlapElozetesBean] persist failed
      javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: mypackage.adat.AdatlapElozetes
       at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
       at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:175)
       at mypackage.adat.AdatlapElozetesBean.persist(AdatlapElozetesBean.java:47)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
       at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:263)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:358)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:398)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:239)
      Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: mypackage.adat.AdatlapElozetes
       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)
       ... 36 more
      

      Can somebody help me, to fix this problem?
      Thank you.

      My configuration is:

      Windows XP,
      Java VM: Java HotSpot(TM) Server VM 1.6.0_01-b06,Sun Microsystems Inc.
      PostgreSQL 8.2
      JBoss [Zion] 4.0.5.GA (build: CVSTag=Branch_4_0 date=200610162339)
      Hibernate 3.2.0.ga
      Hibernate EntityManager 3.2.0.GA
      Hibernate Annotations 3.2.0.GA