写了两个类: Subject(一端) Comment(多端)
其对应hbm xml(截取)为:
subject.hbm.xml:
[color=blue]
name="comments"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>
usage="read-write"
/>
<key
column="subjectid"
/>
<one-to-many
class="com.module.Comment"
/>
</set>[/color]
//***************************************
comment.hbm.xml:
[color=blue]
name="subject"
class="com.module.Subject"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="subjectid"
/>[/color]
[color=red]问题:当删除subject时提示:[/color]
- SQL Error: 1451, SQLState: 23000
- Duplicate key or integrity constraint violation, message from server: "Cannot delete or update a parent row: a foreign key constraint fails (
lnc/t_comment
, CONSTRAINTFKF437E194F69C747
FOREIGN KEY (subjectid
) REFERENCESt_subject
(id
))" - Could not synchronize database state with session
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not delete: [com.module.Subject#32769]; SQL []; Duplicate key or integrity constraint violation, message from server: "Cannot delete or update a parent row: a foreign key constraint fails (
lnc/t_comment
, CONSTRAINTFKF437E194F69C747
FOREIGN KEY (subjectid
) REFERENCESt_subject
(id
))"; nested exception is java.sql.SQLException: Duplicate key or integrity constraint violation, message from server: "Cannot delete or update a parent row: a foreign key constraint fails (lnc/t_comment
, CONSTRAINTFKF437E194F69C747
FOREIGN KEY (subjectid
) REFERENCESt_subject
(id
))" java.sql.SQLException: Duplicate key or integrity constraint violation, message from server: "Cannot delete or update a parent row: a foreign key constraint fails (lnc/t_comment
, CONSTRAINTFKF437E194F69C747
FOREIGN KEY (subjectid
) REFERENCESt_subject
(id
))" at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272) at com.mysql.jdbc.Connection.execSQL(Connection.java:2236) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1588) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105) at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.persister.EntityPersister.delete(EntityPersister.java:581) at net.sf.hibernate.impl.ScheduledDeletion.execute(ScheduledDeletion.java:29) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2397) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261) [color=red]请碰到过这个问题的朋友帮我看看(inverse="false" 时可以删除‘一端’,但我想把维护交给‘多端‘Comment)?[/color]