Hibernate异常No row with the given identifier exists 解决方法


产生此问题的原因:

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

现在hibernate配置可以基于xml配置文件和注解方式,这两种方式都能发生这个异常

xml配置文件解决方法:

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

修改后的:
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" not-found="ignore">
   <column name="userId" unique="true"/>
</many-to-one>
红色是修改的部分。意思是当对应的数据不存在时 忽略掉,用null值填充。该属性默认值:exception 。

注解配置解决方法:

使用hibernate 注解配置实体类的关联关系,在many-to-one,one-to-one关联中,一边引用自另一边的属性,如果属性值为某某的数据在数据库不存在了,hibernate默认会抛出异常。解决此问题,加上如下注解就可以了:

@NotFound(action=NotFoundAction.IGNORE),意思是找不到引用的外键数据时忽略,NotFound默认是exception

下面贴出hibernate 注解的实例代码
@Entity  
@Table(name = "ICT_COMPUTER_LOCATION")  
public class IctComputerLocation {  
    private static final long serialVersionUID = 1L;  
  
    private Integer id;  
    /** IDC编号 */  
    private String idcNum;  
  
    private Integer ictBaseId;  
  
    /** IctBase实体类 */  
    private IctBase ictBase;  
  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "ID")  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    @Column(name = "IDC_NUM")  
    public String getIdcNum() {  
        return idcNum;  
    }  
  
    public void setIdcNum(String idcNum) {  
        this.idcNum = idcNum;  
    }  
  
    @Column(name = "ICT_BASE_ID")  
    public Integer getIctBaseId() {  
        return ictBaseId;  
    }  
  
    public void setIctBaseId(Integer ictBaseId) {  
        this.ictBaseId = ictBaseId;  
    }  
  
    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name = "ICT_BASE_ID", referencedColumnName = "ID", unique = false, nullable = false, insertable = false, updatable = false)  
    @NotFound(action=NotFoundAction.IGNORE)  
    public IctBase getIctBase() {  
        return ictBase;  
    }  
  
    public void setIctBase(IctBase ictBase) {  
        this.ictBase = ictBase;  
    }  


  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值