关于HIbernate多对多映射报错Cannot add or update a child row:

有四个表,一个是订单表,一个水果(商品)表,一个经手人表,一个订单详细信息表(相当于管理两张表单的关系表)
订单详细表分别有字段: 订单ID,商品名称,商品价格,经手人 ,商品数量,商品小计价格。前4样分别是与订单表(oid),水果表(fname,fprice),经手人表(hname)外键约束。

但是在myeclipse中插入不了订单详细信息表的信息,其他可以。但用SQL语句在数据库中可以实现插入。

然后插入的时候出现以下异常:

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.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at dao.OrderdetailDAO.save(OrderdetailDAO.java:24)
at test2.main(test2.java:37)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`test`.`orderdetail`, CONSTRAINT `fk_orderdetail_fname` FOREIGN KEY (`fname`) REFERENCES `fruit` (`fname`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 9 more


在花了一天时间找资料后得出结果,<many-to-one>缺少了property-ref="" 指明要关联的唯一索引属性名

在订单详细信息表的配置(orderdetail.hbm.xml)中应该如下配置

 <many-to-one name="fruitByFprice" class="vo.Fruit"  property-ref="fprice">
        	 <column name="fprice"  not-null="true"/>
        </many-to-one>
这里只是把其中之一的的属性,其他同理。
出现的具体问题这参考这里

http://bbs.csdn.net/topics/391934910?page=1#post-401041762 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值