我有可以包含其他项目的项目,因此我需要与同一实体的@OneToMany关系。我已经像这样配置了我的字段:
@ManyToOne(targetEntity=PersistentItem.class, fetch=FetchType.EAGER)
@JoinColumn(name="ID_CHILDREN", nullable=false)
HashSet<PersistentItem> children=new HashSet<PersistentItem>();
但是,Java总是抛出以下错误:
SEVERE: Servlet.service() for servlet HelloWorld threw exception
javax.persistence.RollbackException: java.lang.NullPointerException
这是因为有些物品没有孩子吗?
我的JPA实现提供程序是EclipseLink。
编辑:完整的堆栈跟踪是:
03-Aug-2009 19:22:09 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet HelloWorld threw exception
javax.persistence.RollbackException: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at info.lenni.gate.servlet.HelloWorld.doGet(HelloWorld.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:637)
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.checkForUnregisteredExistingObject(UnitOfWorkImpl.java:678)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4007)
at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:743)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:1511)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4019)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:182)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:628)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1387)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:160)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1060)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
... 16 more
答案 0 :(得分:2)
您的映射绝对不正确
首先,它应该是@OneToMany
,而不是“@ManyToOne” - 你正在映射一个集合(即很多)。也许这是一个错字,因为问题标题确实说“OneToMany”。
其次,它的映射方式会使关联单向。这不是推荐的方法。您 100%肯定是您想要的吗?最好通过添加“父”属性将其更改为双向映射。
最后,当你选择一个项目(例如项目本身,它的孩子,他们的孩子等等)时,渴望获取这样的关联会拉动整个子树。在某些可能需要的情况下,它们并不常见。你真的想要这种行为吗?也许最好使用不同的模型(比如嵌套集)?
答案 1 :(得分:0)
你打算将nullable设为false是什么意思?
@JoinColumn(name="ID_CHILDREN", nullable=false)
如果有些物品不能生孩子那是对的吗?