hibernate报错 No row with the given identifier exists

昨天遇到了No row with the given identifier exists的错误,网上查找原因都说是有两张表,table1table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique="true">(特殊的多对一映射,实际就是一对一)来关联table2.当hibernate查找的时候,table2里的数据没有与table1相匹配的。

解决方式有两种,

1、配置文件 增加 not-found="ignore"

  1. <many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" not-found="ignore">  
  2.    <column name="userId" unique="true"/>  
  3. </many-to-one> 

2、注解方式 增加 @NotFound(action=NotFoundAction.IGNORE)

其实原因确实是查询关联没找到相应的结果,我们的入手点也应该是分析为什么没有查询到结果,但是通过上述修改并没有解决我的问题。

原因最后发现是由于我的代码不仅仅是两个表关联,其中dept部门表还是一棵树,虽然table1和table2的关联查询有结果,但是table1,即dept表的父节点查询结过不对,导致报上错误。

总结此问题还是要分析为什么没有查询结果。找到没有查询结果的原因在修改

猜你喜欢

转载自1010934061.iteye.com/blog/2173821