我有三个与一对多相关的实体:
订单(一):
@Entity
@Table(name="order")
public class Order implements Serializable {
[...]
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.LAZY, orphanRemoval = true)
private List <OrderDetail> orderDetail;
[...]
}
订单明细(很多):
@Entity
@Table(name="orderDetail")
public class OrderDetail implements Serializable {
[...]
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id", nullable = false)
private Order order;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.LAZY, orphanRemoval = true)
private List <OrderDetailRow> orderDetailRow;
[...]
}
订单明细行(很多):
@Entity
@Table(name="orderDetailRow")
public class OrderDetailRow implements Serializable {
[...]
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_detail_id", nullable = false)
private OrderDetail order;
[...]
}
我生成了分离对象树,假设订单带有一个OrderDetail,其中订单的ID和OrderDetail的ID 与数据库中存在的ID匹配和一些OrderDetailRows(我希望这些对象是在OrderDetail下创建了它们的id == null,因此它是由序列生成的。
然后我发出entityManager.merge(order),Hibernate试图将OrderDetail id设置为0,这导致数据完整性违规异常。
注意:在issung entityManager.merge之后,我看到生成的托管对象树非常好 - OrderDetailRows对象正确生成了它们的ID。我看到Hibernate尝试刷新时出错,因此事务可以完成
我做错了什么?
发布了“更新orderDetail set id =?where ID =?;”的Stacktrace:
"http-apr-4086-exec-3@10191" daemon prio=5 tid=0x52 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:61)
at org.hibernate.persister.collection.OneToManyPersister.writeIndex(OneToManyPersister.java:251)
at org.hibernate.persister.collection.OneToManyPersister.insertRows(OneToManyPersister.java:192)
at org.hibernate.action.internal.CollectionUpdateAction.execute(CollectionUpdateAction.java:102)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy141.save(Unknown Source:-1)