Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-2505

OpenJPA PersistenceException: LongId cannot be cast to MyEntityClass… @OneToMany in combination with FetchType.EAGER

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.0, 2.2.2, 2.3.0, 2.4.0
    • Fix Version/s: 2.2.3, 2.4.0
    • Component/s: jpa
    • Labels:
      None
    • Environment:
      Java 8 and 6

      Description

      When I try to get an object from my database via entity manager that includes a further object that is annotated with @OneToMany and the parameter "fetch = FetchType.EAGER", I got the following exception (below are more Information about my entities and testcases):

      [ERROR 15:50:40] projectNameRestController.catchEmAll(75) | general error occurred
      <openjpa-2.2.2-r422266:1468616 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: org.apache.openjpa.util.LongId cannot be cast to de.projectName.domain.test.Parent
      FailedObject: select p from Parent p [java.lang.String]
      at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1027)
      at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:926)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1033)
      at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
      at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2381)
      at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.loadElement(RelationToManyInverseKeyFieldStrategy.java:90)
      at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.loadElement(RelationCollectionInverseKeyFieldStrategy.java:76)
      at org.apache.openjpa.jdbc.meta.strats.StoreCollectionFieldStrategy.processEagerParallelResult(StoreCollectionFieldStrategy.java:313)
      at org.apache.openjpa.jdbc.meta.strats.StoreCollectionFieldStrategy.loadEagerParallel(StoreCollectionFieldStrategy.java:247)
      at org.apache.openjpa.jdbc.meta.FieldMapping.loadEagerParallel(FieldMapping.java:916)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1106)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1059)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:636)
      at org.apache.openjpa.kernel.DelegatingStoreManager.load(DelegatingStoreManager.java:117)
      at org.apache.openjpa.kernel.ROPStoreManager.load(ROPStoreManager.java:78)
      at org.apache.openjpa.kernel.StateManagerImpl.loadFields(StateManagerImpl.java:3108)
      at org.apache.openjpa.kernel.StateManagerImpl.load(StateManagerImpl.java:446)
      at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:963)
      at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:926)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1033)
      at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
      at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2381)
      at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:274)
      at org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:60)
      at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
      at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1251)
      at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007)
      at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
      at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
      at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
      at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:286)
      at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
      at de.projectName.dao.impl.AccountingDaoImpl.getDistributionsByProvider(AccountingDaoImpl.java:217)
      at de.projectName.dao.impl.AccountingDaoImpl.getDistributionsByProvider(AccountingDaoImpl.java:171)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy27.getDistributionsByProvider(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy28.getDistributionsByProvider(Unknown Source)
      at de.projectName.controller.rest.AccountingprojectNameRestController.getAllUnaccountedDistributions(AccountingprojectNameRestController.java:96)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
      at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
      at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
      at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
      at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
      at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
      at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
      at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
      at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
      at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
      at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
      at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
      at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:744)
      Caused by: java.lang.ClassCastException: org.apache.openjpa.util.LongId cannot be cast to de.projectName.domain.test.Parent
      at de.projectName.domain.test.Child.pcReplaceField(Child.java)
      at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3213)
      at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2643)
      at org.apache.openjpa.kernel.StateManagerImpl.storeObject(StateManagerImpl.java:2633)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setMappedBy(JDBCStoreManager.java:478)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:404)
      at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:306)
      at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)
      at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
      at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1046)
      at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1004)
      ... 126 more
      ________________________________________________

      I created 2 new entities for my tests, they look like this:

      @Entity
      @Table(name = "parent")
      public class Parent {

      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "parentGen")
      @TableGenerator(name = "parentGen", table = "jpa_sequence", pkColumnName = "ID",
      pkColumnValue = "parent", valueColumnName = "SEQUENCE_VALUE", allocationSize = 1)
      public long oid;

      @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
      private List<Child> children;

      public long getOid() {
      return oid;
      }

      public void setOid(long oid) {
      this.oid = oid;
      }

      public List<Child> getChildren() {
      return children;
      }

      public void setChildren(List<Child> children) {
      this.children = children;
      }
      _______________________________________

      @Entity
      @Table(name = "child")
      public class Child {

      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "childGen")
      @TableGenerator(name = "childGen", table = "jpa_sequence", pkColumnName = "ID",
      pkColumnValue = "child", valueColumnName = "SEQUENCE_VALUE")
      public long oid;

      @ManyToOne
      private Parent parent;

      public long getOid() {
      return oid;
      }

      public void setOid(long oid) {
      this.oid = oid;
      }

      public Parent getParent() {
      return parent;
      }

      public void setParent(Parent parent) {
      this.parent = parent;
      }
      ____________________________________

      Further information:

      My jpql: select p from Parent p
      If I change the FetchType to "LAZY" (or leave it just out) I got my result normaly and the children object (the list) is null of course, but if I call the getter of the list now (in the transaction), the object will be filled normaly without this exception.
      Furthermore this exception does not occur when I have just 1 parent with child objects. I can have several parents and children in my tables, but when more than 1 parent have children referenced in the database, this exception will be thrown.
      Also if I change the type of "oid" from "long" to "int" the exception will change, too: "...LongId cannot be cast... -> ...IntId cannot be cast..."
      I used: Java 8 with jpa 2.1.0 and 2.2.2, Java 6 with jpa 2.2.2
      MySQL Database
      Apache Tomcat

      1. openjpa-2505.patch
        1 kB
        Rick Curtis
      2. LongId Scenarios.xlsx
        9 kB
        Lucas Wright
      3. bugreport.zip
        12 kB
        Jostein Gogstad
      4. openjpa-2505.ut.patch
        6 kB
        Rick Curtis

        Activity

        Hide
        curtisr7 Rick Curtis added a comment -

        Will you also post the contents of your persistence.xml file?

        Show
        curtisr7 Rick Curtis added a comment - Will you also post the contents of your persistence.xml file?
        Hide
        curtisr7 Rick Curtis added a comment -

        Attaching a unit test that I created, but was unable to recreate the bug. I changed the annotations on the ID fields, but I don't believe that was relevant to the problem. I suspect that I as missing some 'special' persistence.xml properties.

        Show
        curtisr7 Rick Curtis added a comment - Attaching a unit test that I created, but was unable to recreate the bug. I changed the annotations on the ID fields, but I don't believe that was relevant to the problem. I suspect that I as missing some 'special' persistence.xml properties.
        Hide
        mw_logentis Martin Wygas added a comment -

        <persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">
        <persistence-unit name="punit">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <properties>
        <property name="openjpa.Log" value="log4j"/>
        <property name="openjpa.jdbc.DBDictionary" value="db2"/>
        </properties>
        </persistence-unit>
        </persistence>

        Show
        mw_logentis Martin Wygas added a comment - <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd " version="1.0"> <persistence-unit name="punit"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <properties> <property name="openjpa.Log" value="log4j"/> <property name="openjpa.jdbc.DBDictionary" value="db2"/> </properties> </persistence-unit> </persistence>
        Hide
        curtisr7 Rick Curtis added a comment -

        I haven't been able to create your problem. I tried on trunk and 2.2.x . Would it be possible for you to try to put something together that easily recreates the problem? Also, please take a look at my unit test to see if I'm doing something wrong with setting the data up.

        Thanks,
        Rick

        Show
        curtisr7 Rick Curtis added a comment - I haven't been able to create your problem. I tried on trunk and 2.2.x . Would it be possible for you to try to put something together that easily recreates the problem? Also, please take a look at my unit test to see if I'm doing something wrong with setting the data up. Thanks, Rick
        Hide
        jpaheath Heath Thomann added a comment -

        Hi Martin! Have you given any thought to Rick's previous request? We'd really like to be able to recreate this issue and provide a fix to you asap.

        Thanks,

        Heath

        Show
        jpaheath Heath Thomann added a comment - Hi Martin! Have you given any thought to Rick's previous request? We'd really like to be able to recreate this issue and provide a fix to you asap. Thanks, Heath
        Hide
        jgogstad Jostein Gogstad added a comment -

        Heath Thomann, Rick Curtis: I managed to reproduce the for a OneToMany the the inverse side has a composite primary key. I boiled down the error and attached a bugreport packaged in a maven project. run mvn test and you'll get the error

        Caused by: java.lang.ClassCastException: org.apache.openjpa.util.LongId cannot be cast to org.apache.openjpa.entities.MyUserEntity
                at org.apache.openjpa.entities.AddressEntity.pcReplaceField(AddressEntity.java)
                at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3216)
                at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2643)
                at org.apache.openjpa.kernel.StateManagerImpl.storeObject(StateManagerImpl.java:2633)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setMappedBy(JDBCStoreManager.java:478)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:404)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:306)
                at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)
                at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
                at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1050)
                at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1008)
        

        OpenJPA behaves really wierd. Here's some observations:

        • You need to select a completely unrelated class from the db in a @PostConstruct
          • If the class (FooEntity) is selected anywhere else, everything works
          • If the FooEntity is not selected at all, everything works
        • The error seem only to occur if the inverse side of a OneToMany has a composite key
        Show
        jgogstad Jostein Gogstad added a comment - Heath Thomann , Rick Curtis : I managed to reproduce the for a OneToMany the the inverse side has a composite primary key. I boiled down the error and attached a bugreport packaged in a maven project. run mvn test and you'll get the error Caused by: java.lang.ClassCastException: org.apache.openjpa.util.LongId cannot be cast to org.apache.openjpa.entities.MyUserEntity at org.apache.openjpa.entities.AddressEntity.pcReplaceField(AddressEntity.java) at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3216) at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2643) at org.apache.openjpa.kernel.StateManagerImpl.storeObject(StateManagerImpl.java:2633) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setMappedBy(JDBCStoreManager.java:478) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:404) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:306) at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112) at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57) at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1050) at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1008) OpenJPA behaves really wierd. Here's some observations: You need to select a completely unrelated class from the db in a @PostConstruct If the class (FooEntity) is selected anywhere else, everything works If the FooEntity is not selected at all, everything works The error seem only to occur if the inverse side of a OneToMany has a composite key
        Hide
        jgogstad Jostein Gogstad added a comment -

        Verified on 2.2.2 and 2.3.0

        Show
        jgogstad Jostein Gogstad added a comment - Verified on 2.2.2 and 2.3.0
        Hide
        logemann Marc Logemann added a comment - - edited

        any news on that? i am a colleague of Martin and most likely i am on the same spot as Martin. I also tried various things w/o success to solve / circumvent it. Currently i am on 2.2.0.

        Any ideas how to workaround this?

        Update: The only sensible workaround is to avoid "eager" and use "join fetch" instead.

        Show
        logemann Marc Logemann added a comment - - edited any news on that? i am a colleague of Martin and most likely i am on the same spot as Martin. I also tried various things w/o success to solve / circumvent it. Currently i am on 2.2.0. Any ideas how to workaround this? Update: The only sensible workaround is to avoid "eager" and use "join fetch" instead.
        Hide
        Lucas_Wright Lucas Wright added a comment - - edited

        I have been working on this same issue with IBM for the past eight months or so. Jostein, I appreciate the test case you provided as it helped me to narrow down the issue much further. Here are some facts I've gathered that have already been forwarded to IBM.

        1.) @PostConstruct does not cause the error. I was able to use the test case to reproduce the error with and without @PostConstruct.
        2.) The statement that a non-related entity must exist is false. I removed the FooEntity entirely from persistence.xml (along with its entity annotations), and I was still able to reproduce the error.
        3.) At least two parents and at least one child record for each parent must exist for the error to occur. Once you delete one of the remaining two children the error no longer occurs.
        4.) An embedded key is not necessary for the error to occur (my entities don’t use them).

        I'm going to attach a spreadsheet of actions I performed using the sql data provided in Jostein's test case.

        One last thing: I have a theory that the order in which objects are retrieved from the database (asc or desc) is a factor also, but as I'm sure you have all seen it's difficult to pinpoint.

        Show
        Lucas_Wright Lucas Wright added a comment - - edited I have been working on this same issue with IBM for the past eight months or so. Jostein, I appreciate the test case you provided as it helped me to narrow down the issue much further. Here are some facts I've gathered that have already been forwarded to IBM. 1.) @PostConstruct does not cause the error. I was able to use the test case to reproduce the error with and without @PostConstruct. 2.) The statement that a non-related entity must exist is false. I removed the FooEntity entirely from persistence.xml (along with its entity annotations), and I was still able to reproduce the error. 3.) At least two parents and at least one child record for each parent must exist for the error to occur. Once you delete one of the remaining two children the error no longer occurs. 4.) An embedded key is not necessary for the error to occur (my entities don’t use them). I'm going to attach a spreadsheet of actions I performed using the sql data provided in Jostein's test case. One last thing: I have a theory that the order in which objects are retrieved from the database (asc or desc) is a factor also, but as I'm sure you have all seen it's difficult to pinpoint.
        Hide
        Lucas_Wright Lucas Wright added a comment - - edited

        Here is the promised spreadsheet of steps I performed (LongId Scenarios.xlsx).

        Show
        Lucas_Wright Lucas Wright added a comment - - edited Here is the promised spreadsheet of steps I performed (LongId Scenarios.xlsx).
        Hide
        curtisr7 Rick Curtis added a comment -

        Lucas –

        I sort of tried out your test project this evening and it worked for me.... with a few modifications.

        • Your test uses Oracle DB and I don't have that on my system so I changed test-ctx.xml to use MySQL.
        • I had to change your p.xml to use openjpa.jdbc.SynchronizeMappings as I don't have any of the tables for your Entities.
        • I had to modify MyUserEntity to use an AUTO generated ID as MySQL doesn't support native sequences.

        Have you tried your test on a DB other than oracle?

        Show
        curtisr7 Rick Curtis added a comment - Lucas – I sort of tried out your test project this evening and it worked for me.... with a few modifications. Your test uses Oracle DB and I don't have that on my system so I changed test-ctx.xml to use MySQL. I had to change your p.xml to use openjpa.jdbc.SynchronizeMappings as I don't have any of the tables for your Entities. I had to modify MyUserEntity to use an AUTO generated ID as MySQL doesn't support native sequences. Have you tried your test on a DB other than oracle?
        Hide
        Lucas_Wright Lucas Wright added a comment -

        Rick -

        I wouldn't think the changes you made would have any affect on the error. I believe the root cause is that OpenJPA isn't handling the mapping of child entities to parent entities correctly in certain scenarios that are purely data-related. That's just a theory, but the results seem to support that.

        Oracle is the only database I have access to at the moment so I haven't tested on other platforms. It sounds like the error still occurred for you on MySQL so unless anyone has the need to try it on another database I believe we've narrowed it down to pure data.

        Show
        Lucas_Wright Lucas Wright added a comment - Rick - I wouldn't think the changes you made would have any affect on the error. I believe the root cause is that OpenJPA isn't handling the mapping of child entities to parent entities correctly in certain scenarios that are purely data-related. That's just a theory, but the results seem to support that. Oracle is the only database I have access to at the moment so I haven't tested on other platforms. It sounds like the error still occurred for you on MySQL so unless anyone has the need to try it on another database I believe we've narrowed it down to pure data.
        Hide
        Lucas_Wright Lucas Wright added a comment -

        I've got some more information that I've found in my testing. It seems that the issue is related to the entity manager itself in conjunction with JPA. In my testing I found that when the data already exists in the database, and the retrieval is performed, the error always occurs. However, if the data is inserted using the entity manager, and then retrieved using the entity manager in the same session the error does not occur.

        It almost feels like there is an association that is built by the entity manager when records are inserted using it, but if it wasn't used to insert the records there's some unknown variable in the equation that causes this issue.

        I tried the following three scenarios to validate my hypotheses:

        1.) Insert entities using Spring’s entity manager and OpenJPA associations and load entities using OpenJPA (no error)
        a. Delete records from database
        b. Restart JVM

        2.) Insert using Native JDBC and load entities using OpenJPA (error occurs)
        a. Delete records from database
        b. Restart JVM

        3.) Insert manually in database and load entites using OpenJPA (error occurs)

        Show
        Lucas_Wright Lucas Wright added a comment - I've got some more information that I've found in my testing. It seems that the issue is related to the entity manager itself in conjunction with JPA. In my testing I found that when the data already exists in the database, and the retrieval is performed, the error always occurs. However, if the data is inserted using the entity manager, and then retrieved using the entity manager in the same session the error does not occur. It almost feels like there is an association that is built by the entity manager when records are inserted using it, but if it wasn't used to insert the records there's some unknown variable in the equation that causes this issue. I tried the following three scenarios to validate my hypotheses: 1.) Insert entities using Spring’s entity manager and OpenJPA associations and load entities using OpenJPA (no error) a. Delete records from database b. Restart JVM 2.) Insert using Native JDBC and load entities using OpenJPA (error occurs) a. Delete records from database b. Restart JVM 3.) Insert manually in database and load entites using OpenJPA (error occurs)
        Hide
        curtisr7 Rick Curtis added a comment -

        I was able to fix the reported problem, but I am still having issues trying to recreate it in a simple JUnit. Please give the attached patch a try to ensure that it works in all of your scenarios.

        Thanks,
        Rick

        Show
        curtisr7 Rick Curtis added a comment - I was able to fix the reported problem, but I am still having issues trying to recreate it in a simple JUnit. Please give the attached patch a try to ensure that it works in all of your scenarios. Thanks, Rick
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1626287 from Rick Curtis in branch 'openjpa/trunk'
        [ https://svn.apache.org/r1626287 ]

        OPENJPA-2505 : Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1626287 from Rick Curtis in branch 'openjpa/trunk' [ https://svn.apache.org/r1626287 ] OPENJPA-2505 : Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel.
        Hide
        curtisr7 Rick Curtis added a comment -

        Committed revision 1626287 to trunk.

        Show
        curtisr7 Rick Curtis added a comment - Committed revision 1626287 to trunk.
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1627059 from Heath Thomann in branch 'openjpa/branches/2.2.x'
        [ https://svn.apache.org/r1627059 ]

        OPENJPA-2505: Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel. Back-ported to 2.2.x Rick Curtis' trunk changes.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1627059 from Heath Thomann in branch 'openjpa/branches/2.2.x' [ https://svn.apache.org/r1627059 ] OPENJPA-2505 : Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel. Back-ported to 2.2.x Rick Curtis' trunk changes.

          People

          • Assignee:
            jpaheath Heath Thomann
            Reporter:
            mw_logentis Martin Wygas
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development