honXian 2010-10-19 10:39
浏览 671
已采纳

hibernate 删除 外键约束

写了两个类: 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, CONSTRAINT FKF437E194F69C747 FOREIGN KEY (subjectid) REFERENCES t_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, CONSTRAINT FKF437E194F69C747 FOREIGN KEY (subjectid) REFERENCES t_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, CONSTRAINT FKF437E194F69C747 FOREIGN KEY (subjectid) REFERENCES t_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, CONSTRAINT FKF437E194F69C747 FOREIGN KEY (subjectid) REFERENCES t_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]
  • 写回答

4条回答 默认 最新

  • xiaobeixiaoben 2010-10-20 10:14
    关注

    在删除subject时,因为你在标签中有inverse="true"让标签失效,这样在一端这方就不能准确获得外键信息,所以直接删除会报错,[color=blue]应该是要先解除关系之后再删除[/color]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)