• org.hibernate.ObjectNotFoundException: No row with the given identifier exists解决办法


    hibernate-取消关联外键引用数据丢失抛异常的设置@NotFound

    hibernate项目里面配了很多many-to-one的关联,后台在查询数据时已经作了健全性判断,但还是经常抛出对象找不到异常:

    org.hibernate.ObjectNotFoundException: No row with the given identifier exists


    因为系统给用户使用过程中库表的数据会常发生变化,最常见的是人员变化,原先引用的User 在库表没了,hibernate 多对一关联,多的这端引用的一的那端,如此引用值在一的那端找不到数据,默认就会抛出异常;而后台判断控制不了此问题。解决办法:
    在many-to-one的这端加上属性:not-found=ignore
    hibernate many-to-one的属性not-found,用来指定引用的外键不存在时如何处理:

    exception(默认)抛出异常
    ignore 忽略

    注解
    
    @NotFound(action=NotFoundAction.IGNORE) 
    实列
    
    @ManyToOne(cascade={CascadeType.PERSIST},targetEntity=QeTopic.class)
    @JoinColumn(name="topic_id",updatable=false)
    @NotFound(action=NotFoundAction.IGNORE)
    private QeTopic qeTopic;
    配置文件
    
    在<many-to-one>中设置not-found="ignore"
    也可以将垃圾数据从数据库删掉

     

  • 相关阅读:
    【docker】更换挂载目录
    【设计】交互走查表
    MySQL常用字符串函数
    VIM_manual
    MySQL操作符
    基础SELECT实例
    MySQL字符集及校对规则的理解
    Linux命令之tar-rsync
    Linux-PATH_环境变量
    MySQL常用数据类型
  • 原文地址:https://www.cnblogs.com/loong-hon/p/10081052.html
Copyright © 2020-2023  润新知