persistence@glassfish.java.net

Can not PERSIST detatched object

From: Pierre Delisle <Pierre.Delisle_at_Sun.COM>
Date: Sun, 15 Jan 2006 22:03:02 -0800

I persist an Entity object using the following code (in a Java SE environment):

        this.product = new Product();

        ... web app adds data to the fields of Product ...

        EntityManager em = DbUtil.getEm();
        em.getTransaction().begin();
        em.persist(product);
        em.getTransaction().commit();
        em.close();
        
This works fine for the first entity saved to the database.
However, when I try to persist a second Product,
I get the following exception:

-----
java.lang.IllegalArgumentException: Can not PERSIST detatched object: crud.models.Product_at_1f2be27.
        oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:3154)
        oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:288)
        oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3131)
        oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:96)
        crud.controllers.Admin.create(Admin.java:29)

-----
The EJB3.0 spec says that:

  A detached entity may result from transaction commit (see section 3.3.3), from transaction rollback (see
  section 3.3.4), from serializing an entity or otherwise passing an entity by value (e.g., to a separate
  application tier, through a remote interface, etc.)

Since 'product' is a new object, how come it is being considered a 'detached entity' when persist()
is called on it?

Thanks for any insight into this,

    -- Pierre