我有两个实体Person和Order由JPA / Hibernate Entity Manager管理。我试图通过如下注释来创建两个实体之间的双向一对多关系:
人:
@Id
@Column(name="PERSON_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PERSON_SEQ")
@SequenceGenerator(name = "PERSON_SEQ", sequenceName = "PERSON_SEQ")
private Integer personId;
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private List<Order> orders = new ArrayList<Order>();
public void addOrder(Order o)
{
getOrders().add(o);
o.setPerson(this);
}
订单:
@ManyToOne
@JoinColumn(name="PERSON_ID")
private Person person;
然后我测试代码:
@Test
@Transactional
@Rollback(value=false)
public void populateTable() {
WatchUtil.startTask("Persist Person");
for (int i = 0; i < 25; i++) {
Person tmpPerson= new Person();
tmpPerson.setName("Person1");
Order order = new Order();
order.setOrderNumber("Order1");
tmpPerson.addOrder(order);
dao.persist(tmpPerson); //calls entitymanager.persist(); does not call flush
}
WatchUtil.endTask();
}
这给了我一个约束异常:
2011-04-19 06:25:12,713 INFO [org.springframework.test.context.transaction.TransactionalTestExecutionListener] - <Began transaction (9): transaction manager [org.springframework.orm.jpa.JpaTransactionManager@1f488f1]; rollback [false]>
2011-04-19 06:25:12,713 DEBUG [org.hibernate.SQL] - <insert into PERSON (PERSON_ID, NAME) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)>
2011-04-19 06:25:12,775 DEBUG [org.hibernate.SQL] - <insert into ORDER (ORDER_NUMBER PERSON_ID, ORDER_ID) values (?, ?, ?, ?, ?, ?)>
2011-04-19 06:25:12,947 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: 2291, SQLState: 23000>
2011-04-19 06:25:12,947 ERROR [org.hibernate.util.JDBCExceptionReporter] - <ORA-02291: integrity constraint (BLUE_SITE.FKE2BCFD5374D88D96) violated - parent key not found>
2011-04-19 06:25:12,947 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: 2291, SQLState: 23000>
2011-04-19 06:25:12,947 ERROR [org.hibernate.util.JDBCExceptionReporter] - <ORA-02291: integrity constraint (BLUE_SITE.FKE2BCFD5374D88D96) violated - parent key not found>
2011-04-19 06:25:12,947 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy32.flush(Unknown Source)
at test.JpaDao.persist(JpaDao.java:37)
at test.PersonJpaDaoTests.populateTable(MemberJpaDaoTests.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (BLUE_SITE.FKE2BCFD5374D88D96) violated - parent key not found
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9119)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 44 more
StopWatch '-- DEBUGGING --': running time (millis) = 0
收到此错误后,我从数据库中删除了约束。两个表都已填充,但父项和子项之间的键未对齐。任何人都可以引导我朝着正确的方向前进吗?我查看了很多帖子,他们声称创建像Person.addOrder(Order o)这样的方法应该可以处理。
值得注意的是,我正在使用Oracle序列生成我的ID。 我还为每个类创建了hashCode和Equals方法。
答案 0 :(得分:2)
在查看数据库中的对象后,我发现了问题。在我的项目的初始设置过程中,我没有使用Sequences,但是我自动生成了DDL。这在数据库中创建了一个触发器。
过了一段时间我决定使用一个序列。包含序列的新配置与旧触发器冲突,产生约束错误。删除这些触发器后,异常就消失了。