我定义了以下属性:
@Entity
@Table(name = "person")
public class Person extends AbstractEntity {
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@JoinTable(name = "person_unit", joinColumns = { @JoinColumn(name = "person_id") }, inverseJoinColumns = { @JoinColumn(name = "unit_id") })
private Set<OrgUnit> units;
}
对OrgUnit
类进行了重新定义,定义为:
@Entity
@Table(name = "orgunit", uniqueConstraints = { @UniqueConstraint(columnNames = {
"parentunit_id", "name" }) })
public class OrgUnit extends AbstractEntity {
...
}
现在,当我尝试删除Person
时,它应该只删除person
和person_unit
表中的行。但相反,Hibernate似乎也尝试删除链接的OrgUnit
,因为我收到以下错误消息:
2011-12-02 10:52:30,527 WARN [ajp-2009-2] JDBCExceptionReporter: SQL Error: 547, SQLState: 23000
2011-12-02 10:52:30,527 ERROR [ajp-2009-2] JDBCExceptionReporter: The DELETE statement conflicted with the REFERENCE constraint 'FKE6A2C42EB024FBA5'. The conflict occured in database TEST, table 'TEST.person_unit', column 'unit_id'.
2011-12-02 10:52:30,527 ERROR [ajp-2009-2] AbstractFlushingEventListener: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [<-- snip -->.server.entities.OrgUnit#337]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2712)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2895)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:259)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:183)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
at <-- snip -->.server.controller.PersonController.deletePerson(PersonController.java:60)
我在这里做错了什么?
答案 0 :(得分:1)
是的,它传播了删除,因为你告诉它用
来做@Cascade({ org.hibernate.annotations.CascadeType.ALL})
ALL
包括REMOVE
。它永远不应该用在ManyToXxx协会上。