What I find is that if I retrieve an entity using an instance of the entity
manager, then retrieve another type of entity using a different instance of
the entity manager, then join the two entities, any attempt to call merge on
the first entity causes OpenJPA to complain that the second entity is
unmanaged. OpenJPA doesn't seem to be smart enough to work out that both
entities are detached and be able to sort them out using merge. I had to
redesign my entire application to handle this. I consider it a severe
limitation in OpenJPA that hibernate does not suffer from.

Chris

-----Original Message-----
From: Rick Curtis [mailto:[email protected]] 
Sent: Tuesday, 1 September 2009 9:37 PM
To: [email protected]
Subject: Re: How to persist then update in same transaction

I would also suggest that you enhance [1] your Entities when you test,
otherwise you test a different code path than what you run in WAS.

Thanks,
Rick

On Tue, Sep 1, 2009 at 8:32 AM, Kevin Sutter <[email protected]> wrote:

> Why are you performing the merge()?  Since you are doing all of this
within
> a single transaction and you are not detaching any of these entities,
there
> is no need to do the merge().  You have already persisted orgType earlier
> in
> your code path.  So, go ahead and set the OwnerOrgId and then complete the
> transaction.  That should work for you.  Let us know the results.
>
> Thanks
> Kevin
>
> On Tue, Sep 1, 2009 at 7:55 AM, Diane J <[email protected]> wrote:
>
> >
> > My DAO tests inherit from Springs AbstractJpaTests which start/commit
the
> > transaction for me.
> > The only differences are that when I deploy to websphere, websphere
> handles
> > the transactions and in test spring does and the other difference is
that
> I
> > enhance the classes when I deploy and donot when testing.
> >
> > I switched out openjpa with hibernate and hibernate handles this code
> fine.
> >
> >
> > Rick Curtis wrote:
> > >
> > > When running your application in JSE mode, are you calling
> > > em.getTransaction().begin(commit())? I'm willing to bet you aren't and
> > > that
> > > is why your test works in JSE, but fails on WAS.
> > >
> > > -Rick
> > >
> > > On Mon, Aug 31, 2009 at 12:48 PM, Diane J <[email protected]>
> wrote:
> > >
> > >>
> > >> I do the following in one transaction:
> > >>
> > >> OrgType orgType = new OrgType();
> > >> getOrgTypeDAO().persist(orgType);
> > >> Organization org = new Organization();
> > >> org.setOrgName("Test");
> > >> org.setOrgTypeId(orgType.getOrgTypeId());
> > >> getOrganizationDAO().persist(org);
> > >> orgType.setOwnerOrgId(org.getOrgId());
> > >> getOrgTypeDAO().merge(orgType);
> > >>
> > >> And the merge throws this exception:
> > >>
> > >>  WTRN0074E: Exception caught from before_completion synchronization
> > >> operation: <openjpa-1.2.1-SNAPSHOT-r422266:686069 nonfatal user
error>
> > >> org.apache.openjpa.persistence.InvalidStateException: The generated
> > value
> > >> processing detected an existing value assigned to this field:
> > >> com.adp.ei9.common.entity.OrgType.orgTypeId.  This existing value was
> > >> either
> > >> provided via an initializer or by calling the setter method.  You
> either
> > >> need to remove the @GeneratedValue annotation or modify the code to
> > >> remove
> > >> the initializer processing.
> > >>        at
> > >>
org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:483)
> > >>        at
> > >>
org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:463)
> > >>        at
> > >>
> > >>
> >
>
org.apache.openjpa.jdbc.kernel.JDBCStoreManager.assignObjectId(JDBCStoreMana
ger.java:682)
> > >>        at
> > >>
> > >>
> >
>
org.apache.openjpa.kernel.DelegatingStoreManager.assignObjectId(DelegatingSt
oreManager.java:134)
> > >>        at
> > >>
> > >>
> >
>
org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.j
ava:519)
> > >>        at
> > >>
> > >>
> >
>
org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:28
23)
> > >>        at
> > >> org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:39)
> > >>        at
> > >>
> > >>
> >
>
org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java
:959)
> > >>        at
> > >> org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1948)
> > >>        at
> > >> org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1908)
> > >>        at
> > >>
> >
>
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1826)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.uow.ComponentContextSynchronizationWrapper.beforeCompletion(Compo
nentContextSynchronizationWrapper.java:65)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.tx.jta.RegisteredSyncs.coreDistributeBefore(RegisteredSyncs.java:289
)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.tx.jta.RegisteredSyncs.distributeBefore(RegisteredSyncs.java:150)
> > >>        at
> > >>
> com.ibm.ws.tx.jta.TransactionImpl.prePrepare(TransactionImpl.java:2222)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.tx.jta.TransactionImpl.stage1CommitProcessing(TransactionImpl.jav
a:486)
> > >>        at
> > >>
com.ibm.tx.jta.TransactionImpl.processCommit(TransactionImpl.java:978)
> > >>        at
> > com.ibm.tx.jta.TransactionImpl.commit(TransactionImpl.java:913)
> > >>        at
> > >> com.ibm.ws.tx.jta.TranManagerImpl.commit(TranManagerImpl.java:369)
> > >>        at
> com.ibm.tx.jta.TranManagerSet.commit(TranManagerSet.java:161)
> > >>        at com.ibm.ejs.csi.TranStrategy.commit(TranStrategy.java:915)
> > >>        at
> com.ibm.ejs.csi.TranStrategy.postInvoke(TranStrategy.java:228)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ejs.csi.TransactionControlImpl.postInvoke(TransactionControlImpl.jav
a:561)
> > >>        at
> > >> com.ibm.ejs.container.EJSContainer.postInvoke(EJSContainer.java:4512)
> > >>        at
> > >>
> > >>
> >
>
com.adp.ei9.esb.test.EJSLocalStatelessJPATester_80c7ee76.testPersistAndMerge
OwnerId(EJSLocalStatelessJPATester_80c7ee76.java:138)
> > >>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > >>        at
> > >>
> > >>
> >
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45
)
> > >>        at
> > >>
> > >>
> >
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:37)
> > >>        at java.lang.reflect.Method.invoke(Method.java:599)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.etools.utc.model.ReflectionMethodModel.invoke(ReflectionMethodModel.
java:65)
> > >>        at
> > >>
> com.ibm.etools.utc.servlet.InvokeServlet.invoke(InvokeServlet.java:113)
> > >>        at
> > >>
> com.ibm.etools.utc.servlet.InvokeServlet.doPost(InvokeServlet.java:374)
> > >>        at
javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
> > >>        at
javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1
443)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.
java:790)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.
java:443)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrap
perImpl.java:175)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServl
etWrapper.java:91)
> > >>        at
> > >>
> >
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:859)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:155
7)
> > >>        at
> > >>
> >
>
com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(Ht
tpInboundLink.java:455)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(Ht
tpInboundLink.java:384)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLRea
dCallback.java:83)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.c
omplete(SSLReadServiceContext.java:1772)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioRea
dCompletionListener.java:165)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java
:217)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture
.java:161)
> > >>        at
com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
> > >>        at
> > com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
> > >>        at
> > >>
> > >>
> >
>
com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766
)
> > >>        at
com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
> > >>        at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)
> > >>
> > >>
> > >> This is expected to have a generated id because I already persisted
> the
> > >> entity. This only fails on websphere and not in my testcase where the
> > >> transactions are not set to managed.
> > >>
> > >> These are my jpa props:
> > >>                                <entry key="openjpa.TransactionMode"
> > >> value="managed" />
> > >>                                <entry
> > key="openjpa.ConnectionFactoryMode"
> > >> value="managed" />
> > >>                                <entry
key="openjpa.jdbc.UpdateManager"
> > >> value="operation-order" />
> > >>
> > >> My test jpa props are:
> > >>                                <entry
> > >> key="openjpa.jdbc.SynchronizeMappings"
> > >> value="buildSchema(ForeignKeys=true)" />
> > >>                                <entry key="openjpa.jdbc.DBDictionary"
> > >>
> > >>
> > >>
> >
>
value="org.apache.openjpa.jdbc.sql.H2Dictionary(useGetObjectForBlobs=false)"
> > >> />
> > >>                                <entry
key="openjpa.jdbc.UpdateManager"
> > >> value="operation-order" />
> > >>
> > >>
> > >> --
> > >> View this message in context:
> > >>
> >
>
http://n2.nabble.com/How-to-persist-then-update-in-same-transaction-tp355506
9p3555069.html
> > >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > >>
> > >
> > >
> >
> > --
> > View this message in context:
> >
>
http://n2.nabble.com/How-to-persist-then-update-in-same-transaction-tp355506
9p3560316.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

Reply via email to