openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kevin Sutter" <>
Subject Re: merge only works with managed entity
Date Mon, 10 Dec 2007 16:24:25 GMT
If I am understanding your scenario correctly, your userProfile input
parameter is an unmanaged entity, but the key for this entity does already
exist in the database.  So, when you attempt to do the merge() processing
(and subsequent transaction commit), you are getting a "duplicate key"
exception from Oracle.

Calling merge() with a new unmanaged entity acts like a persist() call and
adds it to the persistence context.  The persist() method is intended for
new entities that do not currently exist in the database.

So, on the surface, your scenario seems to be acting as I would expect with
any JPA implementation.  But, you have indicated that JBoss' Hibernate is
processing as you had hoped.  Based on my understanding of the spec and your
scenario, I think you are getting lucky with Hibernate.

I would suggest doing the find() operation first to properly load the entity
into the persistence context and then do your updates.  You can always
detach this entity from the persistence context, if necessary, for other
processing and then merge it back in later.  Since the merge() would be
operating on a known, detached entity, an update operation will be performed
instead of the insert.

A few other observations...  Remember that the merge() operation returns the
managed entity as a return value.  The original entity that you passed into
the merge() method is not managed.  So, if you want any state changes to the
entity to be properly maintained and persisted, you need to use the returned
managed instance of the entity from the merge() operation.

You also mentioned how the nested properties are not managed.  This may be
related to the above observation.  Another possibility is that you need to
declare whether you want the merge processing to be cascaded to the objects
pointed to by the entity.  You would specify this on the relationship
annotation via the cascade=CascadeType.MERGE element.  You haven't provided
the complete entity definition, so I'm not exactly sure how you have these
entity types defined.

Hope this information helps.

Good luck,

On Dec 7, 2007 11:50 AM, jackson12 <> wrote:

> Thanks Kevin for your prompt response.
> Here is a simple use case we have:
> within an EJB , we have the following method:
> public UserProfile updateUserProfile(UserProfile userProfile) {
>                return getProfileDAO().update(userProfile);
> }
> when this method is called, the input parameter has all of the values
> including the key, but we got a runtime exception because it's trying to
> insert userProfile into database.
> org.apache.openjpa.persistence.PersistenceException: ORA-00001: unique
> constraint (VPDNGDITR17.PK_SECURITY_USER_PROFILE) violated FailedObject:
> MODIFIED_BY_USER, ........
> After we change the code to the following, it works fine:
>        public UserProfile updateUserProfile(UserProfile userProfile) {
>                UserProfileDAO profileDAO = getProfileDao();
>                UserProfile tmpUserProfile =
> profileDAO.getUserProfile(userProfile.getName());
>                PropertyUtils.copyProperties(tmpUserProfile, userProfile);
>                return getProfileDao().update(tmpUserProfile);
>        }
> Here is the update method of ProfileDAO:
> public T update(T entity)
> {
>       getEntityManager().merge(entity);
> }
> When the input parameter has nested object, even the above approach won't
> work any more, because PropertyUtils.copyProperties will make the nested
> object not managed anymore.
> Thanks a lot
> Kevin Sutter wrote:
> >
> > jackson12,
> > Could you be more specific with your example?  We have various testcases
> > that do this exact process of merging in non-managed entities (without
> > first
> > retrieving the entity from the DB).   Could you further explain your
> test
> > scenario and what results you are getting?
> >
> > Thanks,
> > Kevin
> >
> > On Dec 7, 2007 11:12 AM, jackson12 <> wrote:
> >
> >>
> >> Hi, We are trying to migrate our JBoss EJB3 application to IBM
> websphere
> >> with
> >> OpenJPA. We noticed in OpenJPA, the merge method does not work with
> >> non-managed entity. You have to retrieve the entity from DB, make some
> >> changes and then call merge. But with JBoss's implementation, you don't
> >> have
> >> to retrieve the entity from DB first. the merge method works fine for
> >> non-managed entity.
> >>
> >> Is there anyway for OpenJPA to work the same way as JBoss's JPA
> >> implementation on this part? or is there a way in OpenJPA to make a
> >> non-managed entity managed?
> >>
> >> thanks in advance
> >> --
> >> View this message in context:
> >>
> >> Sent from the OpenJPA Developers mailing list archive at
> >>
> >>
> >
> >
> --
> View this message in context:
> Sent from the OpenJPA Developers mailing list archive at

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message