Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

ManyToOne persist fails after migration to OpenJPA

 
Greenhorn
Posts: 5
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
have some odd trobles after migration from hibernate to openjpa
Simple many-to-one relation

Each Answer aggregates Question it belongs to.




workflow: when new user login, new Answer object is created () but aggregated Question object is retrieved from DB and set to Answer manually (suitable for JSF linking). Answer is to be persisted and linked to it's question.

DAO:

stacktrace:
org.apache.openjpa.persistence.EntityExistsException: An object of type "org.apache.openjpa.enhance.com$bsuir$survey$model$Question$pcsubclass" with oid "com.bsuir.survey.model.Question-2" already exists in this context; another cannot be persisted.
FailedObject: org.apache.openjpa.enhance.com$bsuir$survey$model$Question$pcsubclass@15dcfa4
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(BrokerImpl.java:4752)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2441)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2362)
at org.apache.openjpa.kernel.SingleFieldManager.persist(SingleFieldManager.java:259)
at org.apache.openjpa.kernel.StateManagerImpl.cascadePersist(StateManagerImpl.java:2894)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2457)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2280)
at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1021)
at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:645)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:346)
at $Proxy27.persist(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:193)
at $Proxy5.persist(Unknown Source)
at com.bsuir.survey.dao.AnswerDao.createAnswerGroup(AnswerDao.java:24)
...

in case entityManager.merge(answer) :

org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "org.apache.openjpa.enhance.com$bsuir$survey$model$Question$pcsubclass@8da5e9". If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.
FailedObject: org.apache.openjpa.enhance.com$bsuir$survey$model$Question$pcsubclass@8da5e9
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2421)
...

the same code worked fine by hibernate, but openjpa is requared. help !
(createQuery via openjpa still works correct, only persist/merge fail)

 
Vladlen Prohorov
Greenhorn
Posts: 5
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
resolved:

some hardcode because of OpenJPA that enforced not to use detached object in setter.
I still don't understant the necessity of such compication compare to hibernate (where just entityManager.persist(answer) is requared)...
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic