Hi mike, Yes, id field with "@GeneratedValue(strategy=GenerationType.AUTO)" annotation is the primary key field and I didn't serialize the entity. I just modified some fields of the entity in the same VM and try to merge the entity into a new context.
Regards, Yu Wang On Tue, Jul 14, 2009 at 9:38 PM, Michael Dick<michael.d.d...@gmail.com> wrote: > Hi Yu, > > This is not expected behavior. You should be able to detach and merge your > entity into a new persistence context. > > The error you're getting is because you have a value specified in a > "GeneratedField" (ie in your entity you have @GeneratedValue on the field). > The field in question is Policy.id (sounds like your Primary Key).. Have you > modified this field after closign the original context? > > If not then it's probably a case of OpenJPA not realizing that the field was > loaded (ie not set after detaching). Did you serialize the entity after > closing the original context (if so there are other properties that you may > need to set). > > -mike > > On Tue, Jul 14, 2009 at 3:21 AM, wang yu <wangy...@gmail.com> wrote: > >> Hello, >> I uses OpenJPA 1.2.1. >> Even if I add @Version annotation for the entity class,It doesn't work. >> Every time the merge method is used, OpenJPA try to insert a new row >> into database. >> >> So, is it a expected behavior of OpenJPA? Merge method cannot attach a >> detached entity into a new context? >> >> Regards, >> Yu Wang >> >> On Mon, Jul 13, 2009 at 5:44 PM, wang yu<wangy...@gmail.com> wrote: >> > Hello, >> > My case is: >> > 1. Find an entity and close the context. >> > 2. Update it with user input. >> > 3. Create a new EntityManager and try to merge it into new context. >> > >> > Then the following exception was thrown: >> > >> > <openjpa-1.2.1-r752877:753278 nonfatal user error> >> > org.apache.openjpa.persistence.InvalidStateException: The generated >> > value processing detected an existing value assigned to this field: >> > com.XXX.bean.Policy.id. 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. >> > >> org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:483) >> > >> org.apache.openjpa.util.ApplicationIds.assign(ApplicationIds.java:463) >> > >> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.assignObjectId(JDBCStoreManager.java:744) >> > >> org.apache.openjpa.kernel.DelegatingStoreManager.assignObjectId(DelegatingStoreManager.java:134) >> > >> org.apache.openjpa.kernel.DelegatingStoreManager.assignObjectId(DelegatingStoreManager.java:134) >> > >> org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:520) >> > >> org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2824) >> > org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:39) >> > >> org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:960) >> > org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1948) >> > >> org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1908) >> > >> org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1826) >> > >> org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81) >> > org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1350) >> > >> org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:877) >> > >> org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:512) >> > com.XXX.dao.DAOContainer.commitTransaction(DAOContainer.java:195) >> > >> com.XXX.policy.PolicyServiceImpl.update(PolicyServiceImpl.java:272) >> > com.XXX.ds.ServletIndex.doGet(ServletIndex.java:78) >> > javax.servlet.http.HttpServlet.service(HttpServlet.java:617) >> > javax.servlet.http.HttpServlet.service(HttpServlet.java:717) >> > >> > What's the root cause of this exception? >> > Thanks! >> > >> > Regards, >> > Yu Wang >> > >> >