JPA, Open JPA OneToMany - FailedObject



我正在做简单的OneToMany JPA概念,并使用Junit进行验证。获取实体时发生Getting FailedObject错误(oneToMany关系).am使用Oen JPA最新版本,TomEE 8。

发布实体

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
}

文章评论

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
}

服务

@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();
}

}

测试后

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查询

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

PostComments(id,review,post_id)VALUES(?,?,?)[params=(int)51,(String)成功评审,(int)1]

选择t0.title、t1.post_id、t1.id、t1.review FROM post t0 LEFT外部加入帖子评论t1 ON t0.id=t1.post_id其中t0.id=?订单依据t1.post_id ASC[params=(int)1]

异常

SEVERE-EjbTransactionUtil.handleSystemException:nullorg.apache.openjpa.persistence.PersistenceException:nullFailedObject:1[org.apache.openjpa.util.IntId][java.lang.String],位于org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1029)org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:923)org.apache.openjpa.kernel.DelegatingBroker.fund(DelegatingBroker.java:230)在org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:495)在org.apache.openejb.persistence.JtaEntityManager.fund(JtaEntityManager.java:224)网址:com.demo.ex.service.PostService.getPost(PostService.java:41)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)org.apache.openejb.core.entercept.ReflectionInvocationContext$Invocation.ioke(ReflectionInvocationContext.java:205)在org.apache.openejb.core.entercept.ReflectionInvocationContext.prough(ReflectionInvocationContext.java:186)在org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:191)在org.apache.openejb.monitoring.StatsInterceptor.ininvoke(StatsInterector.java:102)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)org.apache.openejb.core.entercept.ReflectionInvocationContext$Invocation.ioke(ReflectionInvocationContext.java:205)在org.apache.openejb.core.entercept.ReflectionInvocationContext.prough(ReflectionInvocationContext.java:186)在org.apache.openejb.core.enterceptor.InterceptorStack.invoke(InterceptorStack.java:85)在org.apache.openejb.core.StatelessContainer._invoke(StatelessContainer.java:252)在org.apache.openejb.core.StatelessContainer.invoke(StatelessContainer.java:212)在org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxy Handler.java:265)在org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxy处理程序.java:260)在org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxy处理程序.java:89)在org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyMandler.java:347)在com.demo.ex.service.PostService$$LocalBeanProxy.getPost(com/demo/ex/service/PostService.java)网址:com.demo.ex.PostTest.testPost(PostTest.java:30)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)位于的junit.framework.TestCase.runTest(TestCase.java:176)位于的junit.framework.TestCase.runBare(TestCase.java:141)位于的junit.framework.TestResult$1.protect(TestResult.java:122)位于的junit.framework.TestResult.runProtected(TestResult.java:142)位于的junit.framework.TestResult.run(TestResult.java:125)junit.framework.TestCase.run(TestCase.java:129)位于的junit.framework.TestSuite.runTest(TestSuite.java:252)junit.framework.TestSuite.run(TestSuite.java:247)org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)网址:org.junit.runner.JUnitCore.run(JUnitCores.java:137)com.intellij.junit4.JUnit4DeaTestRunner.startRunnerWithArgs(JUnit4DieTestRunner.java:68)在com.intellij.rt.execution.junit.IideaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)由处的java.lang.NullPointerException引起org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setInverseRelation(JDBCStoreManager.java:452)在org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:412)在org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager/java:305)在org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)在org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)在org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1048)在org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1006)…还有46个

javax.ejb.EJBException:bean遇到非应用程序例外嵌套异常为:org.apache.openjpa.persistence.PersistenceException:null失败对象:1[org.apache.openjpa.util.IntId][java.lang.String]

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

依赖

<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>

似乎在运行代码之前没有增强实体,因此某些功能无法完全发挥作用:http://openjpa.apache.org/enhancement-with-maven.html

或者,您可以在测试JVM上设置openejb-javaagent(通过surefire参数,您可以像中那样将-javaagent:/path/to/openejb-javanagent.jar传递给http://tomee.apache.org/javaagent.html)它将执行相同的操作(字节码插入),但在运行时与普通的大部头一样

附带说明:如果您不需要跨多个提供商移植此代码,则建议进行构建时增强

罗曼

我刚刚用Hibernate提供程序尝试了相同的代码。它运行良好。同一代码不适用于Open JPA:(

Persistence.xml

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

依赖性

<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>openejb-core-hibernate</artifactId>
<version>8.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>test</scope>
</dependency>

最新更新