2 Replies Latest reply on Jan 17, 2006 8:49 PM by Quande Ren

    Problem org.hibernate.PersistentObjectException: detached en

    gianfeng lee Newbie

      Hello, please see my persistent classes below:
      1-User.java

      package com.liefallow.member.entiy;
      
      import java.io.Serializable;
      
      import javax.persistence.*;
      
      @Entity
      @Table(name = "user")
      public class User implements Serializable {
      
       private static final long serialVersionUID = -4063362489253613429L;
      
       private int userId;
      
       private String userName;
      
       //Other variables
      
       public User() {
       }
      
       @Id(generate = GeneratorType.AUTO)
       @Column(name = "userId")
       public int getUserId() {
       return userId;
       }
      
       /**
       * @param userId
       * The userId to set.
       */
       public void setUserId(int userId) {
       this.userId = userId;
       }
      
      
       /**
       * @return Returns the userName.
       */
       public String getUserName() {
       return userName;
       }
      
       /**
       * @param userName
       * The userName to set.
       */
       public void setUserName(String userName) {
       this.userName = userName;
       }
      
       //other getter and setter methods
      
      
      }
      

      2-RemoteOperate.java
      import javax.ejb.Remote;
      
      import com.liefallow.member.entiy.User;
      
      @Remote
      public interface RemoteOperate {
      
       public void addUser(User user);
      
      }
      

      3-OperateBean.java
      @Stateless
      @Local( { Operate.class })
      @Remote( { RemoteOperate.class })
      public class OperateBean implements Operate, RemoteOperate {
      
       @PersistenceContext(unitName = "opt")
       protected EntityManager em;
      
       public void addUser(User user) {
       em.persist(user);
       }
      

      4-Test.java
      public class Test {
      
       @SuppressWarnings("unchecked")
       public static void main(String[] args) {
      
       Hashtable env = new Hashtable();
       env.put("java.naming.factory.initial",
       "org.jnp.interfaces.NamingContextFactory");
       env.put("java.naming.factory.url.pkgs",
       "org.jboss.naming:org.jnp.interfaces");
       env.put("java.naming.provider.url", "jnp://localhost:1099");
      
       RemoteOperate opt = null;
       try {
       InitialContext ctx = new InitialContext(env);
       System.out.println(RemoteOperate.class.getName());
       opt = (RemoteOperate) ctx.lookup(RemoteOperate.class.getName());
       } catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      
       User user = new User();
       user.setUserId(1);
       user.setUserName("gianfeng");
       user.setPasswd("123456");
       user.setRegTime("2005-09-14");
       user.setEmail("gianfeng@qianren.com");
       user.setRegIp("127.0.0.1");
       user.setSex("?");
      
       opt.addUser(user);
      
       System.out.println("ss");
       }
      
      }
      



      I get the exception :
      2005-09-15 13:46:11,342 INFO [STDOUT] org.hibernate.PersistentObjectException: detached entity passed to persist: com.liefallow.member.entiy.User
       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.persist(SessionImpl.java:531)
       at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:129)
       at org.jboss.ejb3.entity.InjectedEntityManager.persist(InjectedEntityManager.java:92)
       at com.liefallow.member.service.OperateBean.addUser(OperateBean.java:20)
       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:585)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:33)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:183)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:69)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:325)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:201)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:259)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:313)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:147)
      


      Can you suggect the way to solver this problem?thanks.