2

I am doing simple OneToMany JPA concept and verifying using Junit. Getting FailedObject error while getting entity ( oneToMany relation ) . am using Oen JPA latest version, TomEE 8.

Post Entity

import javax.persistence.*;
import java.util.List;

@Entity
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String title;

    @OneToMany(mappedBy = "post", targetEntity = PostComments.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<PostComments> postCommentsList;

  //Getter & Setter
}

PostComments

import javax.persistence.*;

@Entity
public class PostComments {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name = "post_id", nullable = false)
    private Post post;

    private String review;

    //Getter & Setter
}

Service

@Stateless
public class PostService {

    @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.TRANSACTION)
    private EntityManager em;


    public void addPost() {
        PostComments postComments = new PostComments();
        postComments.setReview("Success Review");

        Post post = new Post();
        post.setTitle(" Welcome");
        postComments.setPost(post);
        List<PostComments> postCommentsList = new ArrayList<>();
        postCommentsList.add(postComments);
        post.setPostCommentsList(postCommentsList);

        em.persist(post);

        System.out.println(" Post has been saved .........");
    }

    public List<Post> getPost() {
        System.out.println("###########"+em);
        int id = 1;
        Post post = em.find(Post.class, id);
        return Arrays.asList(post);
        //return em.createQuery("select e from Post as e").getResultList();
    }


}

PostTest

public class PostTest extends TestCase {

    @EJB
    private PostService postService;

    protected void setUp() throws Exception {
        final Map p = new Properties();
        p.put("movieDatabase", "new://Resource?type=DataSource");
        p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
        EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
    }

    @Test
    public void testPost() {
        postService.addPost();
        List<Post> postList = postService.getPost();
        System.out.println(" postList = "+postList);
        assertEquals(1,postList.size());
        System.out.println(postList.get(0).getTitle()+" "+postList.get(0).getPostCommentsList().get(0).getReview());
    }



}

JPA Query

INSERT INTO Post (id, title) VALUES (?, ?) [params=(int) 1, (String) Welcome]

PostComments (id, review, post_id) VALUES (?, ?, ?) [params=(int) 51, (String) Success Review, (int) 1]

SELECT t0.title, t1.post_id, t1.id, t1.review FROM Post t0 LEFT OUTER JOIN PostComments t1 ON t0.id = t1.post_id WHERE t0.id = ? ORDER BY t1.post_id ASC [params=(int) 1]

Exception

SEVERE - EjbTransactionUtil.handleSystemException: null org.apache.openjpa.persistence.PersistenceException: null FailedObject: 1 [org.apache.openjpa.util.IntId] [java.lang.String] at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1029) at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:923) at org.apache.openjpa.kernel.DelegatingBroker.find(DelegatingBroker.java:230) at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:495) at org.apache.openejb.persistence.JtaEntityManager.find(JtaEntityManager.java:224) at com.demo.ex.service.PostService.getPost(PostService.java:41) 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:498) at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205) at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186) at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:191) at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:102) 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:498) at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205) at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186) at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85) at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:252) at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:212) at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:265) at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:260) at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:89) at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:347) at com.demo.ex.service.PostService$$LocalBeanProxy.getPost(com/demo/ex/service/PostService.java) at com.demo.ex.PostTest.testPost(PostTest.java:30) 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:498) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141) at junit.framework.TestResult$1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java:125) at junit.framework.TestCase.run(TestCase.java:129) at junit.framework.TestSuite.runTest(TestSuite.java:252) at junit.framework.TestSuite.run(TestSuite.java:247) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.NullPointerException at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setInverseRelation(JDBCStoreManager.java:452) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:412) at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:305) 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:1048) at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1006) ... 46 more

javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: org.apache.openjpa.persistence.PersistenceException: null FailedObject: 1 [org.apache.openjpa.util.IntId] [java.lang.String]

at org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(BaseEjbProxyHandler.java:447) at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:351) at com.demo.ex.service.PostService$$LocalBeanProxy.getPost(com/demo/ex/service/PostService.java)

Dependency

<dependency>
    <groupId>org.apache.tomee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.apache.tomee</groupId>
    <artifactId>openejb-core</artifactId>
    <version>8.0.0-M1</version>
    <scope>test</scope>
</dependency>
Gnana
  • 2,130
  • 5
  • 26
  • 57
  • Are you sure about `Post` id = 1? Might be another value – XtremeBaumer Oct 31 '18 at 14:44
  • Yes, post id =1. SELECT t0.title, t1.post_id, t1.id, t1.review FROM Post t0 LEFT OUTER JOIN PostComments t1 ON t0.id = t1.post_id WHERE t0.id = ? ORDER BY t1.post_id ASC [params=(int) 1] – Gnana Oct 31 '18 at 15:40
  • I mean in the database and not in the query – XtremeBaumer Oct 31 '18 at 15:42
  • Yes, INSERT INTO Post (id, title) VALUES (?, ?) [params=(int) 1, (String) Welcome] PostComments (id, review, post_id) VALUES (?, ?, ?) [params=(int) 51, (String) Success Review, (int) 1] – Gnana Oct 31 '18 at 15:43
  • Have you tried [this](https://stackoverflow.com/questions/38087792/org-apache-openjpa-persistence-persistenceexception-null)? – XtremeBaumer Oct 31 '18 at 15:45
  • using latest version OpenJPA 3.0.0. that post has old version – Gnana Oct 31 '18 at 16:01

2 Answers2

0

seems you didn't enhance your entities before running the code so some features are not fully functional: http://openjpa.apache.org/enhancement-with-maven.html

alternatively you can set openejb-javaagent on the test JVM (through surefire args you can pass -javaagent:/path/to/openejb-javaagent.jar like in http://tomee.apache.org/javaagent.html) which will do the same (bytecode instrumentation) but at runtime as in a plain tomee

side note: build time enhancement is recommended if you don't need to have this code portable accross multiple providers

Romain

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13
-1

I have just tried the same code with Hibernate provider. it is working good. same code not working with Open JPA :(

Persistence.xml

<provider>org.hibernate.ejb.HibernatePersistence</provider>

Dependency

<dependency>
    <groupId>org.apache.tomee</groupId>
    <artifactId>openejb-core-hibernate</artifactId>
    <version>8.0.0-SNAPSHOT</version>
    <type>pom</type>
    <scope>test</scope>
</dependency>
Gnana
  • 2,130
  • 5
  • 26
  • 57