我正在使用Eclipselink来保留以下实体(遗漏了无关紧要的代码):
@Entity
public class TestEntity {
@Id
@GeneratedValue
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="testEntity")
private List<OtherTestEntity> other = new ArrayList<OtherTestEntity>();
}
@Entity
@IdClass(OtherTestEntityPK.class)
public class OtherTestEntity {
public static class OtherTestEntityPK {
public int testEntity;
public int id;
}
@Id
@ManyToOne
private TestEntity testEntity;
@Id
@GeneratedValue
private int id;
@ManyToOne
private OtherTestEntity parent;
@OneToMany(mappedBy="parent")
private List<OtherTestEntity> childs = new ArrayList<OtherTestEntity>();
}
在尝试保留TestEntity时,我收到以下错误:
javax.persistence.PersistenceException: Exception [EclipseLink-28019] Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [anaflow] failed. Close all factories for this PersistenceUnit.
Internal Exception: java.lang.StackOverflowError
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:869)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:809)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at com.sap.anaflow.analysis.model.persistence.PersistenceManager.persist(PersistenceManager.java:17)
at com.sap.anaflow.analysis.model.persistence.PersistenceTest.testPersistence(PersistenceTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [anaflow] failed. Close all factories for this PersistenceUnit.
Internal Exception: java.lang.StackOverflowError
at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:239)
... 30 more
Caused by: java.lang.StackOverflowError
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.getMappingsByField(ObjectBuilder.java:3508)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.getMappingForField(ObjectBuilder.java:3486)
at org.eclipse.persistence.mappings.OneToOneMapping.getFieldClassification(OneToOneMapping.java:890)
at org.eclipse.persistence.mappings.OneToOneMapping.getFieldClassification(OneToOneMapping.java:894)
at org.eclipse.persistence.mappings.OneToOneMapping.getFieldClassification(OneToOneMapping.java:894)
----- Many, many more of these -----
除了Error之外,我想知道如何定义一个OtherTestEntity的子节点和父节点具有相同TestEntity的约束,这样数据库只需要保存没有外键的单个id映射。 这是必要的,因为我的真实项目包含5个外键的组合。