persistence@glassfish.java.net

RE: Can not PERSIST detatched object

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Mon, 16 Jan 2006 08:24:13 -0500

Hello Pierre,
        Please file a P3 issue against the entity-perssitence module to have this string updated. A suggestion of what you feel would be a better message would be helpful.
--Gordon

-----Original Message-----
From: Pierre.Delisle_at_Sun.COM [mailto:Pierre.Delisle_at_Sun.COM]On Behalf Of
Pierre Delisle
Sent: Monday, January 16, 2006 1:46 AM
To: persistence_at_glassfish.dev.java.net
Subject: Re: Can not PERSIST detatched object


Thanks for the quick reply Marina.

I had not specified @GeneratedValue to the PK and that caused the problem.
[Again, I wish the error messages were a bit more verbose to help
developers recover easily from pilot errors...]

All is fine now.

Thanks again,

    -- Pierre

Pierre Delisle wrote:

> 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
>