【问题标题】:org.hibernate.ObjectNotFoundException: No row with the given identifier exists: Single table queryorg.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:单表查询
【发布时间】:2012-04-03 22:15:07
【问题描述】:

我正在使用休眠进行一个简单的查询。没有连接。我要做的就是从表中检索最大 id。该服务运行了几个月,但突然,在过去的两周内,我收到了可怕的 No row with the given identifier exists 错误。即使此表包含数百万行。怎么会这样?

这是进行查找的服务:

try {
    session = HibernateUtils.beginTransaction("mydatabase");
    criteria = session.createCriteria(MyClass.class);
    criteria.setMaxResults(1);
    Order order = Order.desc("id");
    criteria.addOrder(order);
    myclass = (MyClass) criteria.uniqueResult();
    } catch (HibernateException e_) {
        e_.printStackTrace();
        String msg = "Problem getting maximum id from myclass table "
            +  e_.getCause();
    LOG.error(msg);
    throw new DataBaseAccessException(msg);
    }finally {
        try {
            HibernateUtils.closeSessions();
        } catch (Exception e_) {
        }
    }

【问题讨论】:

    标签: java sql-server hibernate


    【解决方案1】:

    即使您在没有连接的情况下对单个表进行查询,查看查询中涉及的底层模型也很重要。如果该模型具有连接列并且关联的外键不存在于连接表中,则休眠将失败。

    在这种情况下,底层模型如下:有一个连接列 cid 映射到另一个表 code_table。但是code_table中没有对应的条目与icdtable中正在查询的值。尽管查询仅在 icdtable 上进行,但底层模型与另一个表连接这一事实要求表之间存在数据完整性,而不仅仅是在执行查询的表中。

    @Entity
    @Table (name="icdtable", catalog="emscribedx")
    public class Icdtable {
    private Long _icdid;
    private Long _cid;
    private String _icdcode; //TODO: add this to hibernate mappings ('PN' or 'NN')
    private String _status;
    private String _create_date;
    private String _kstatus;
    private int _enterorder;
    private String _poa;
    private String _ppoa;
    private String _userid;
    private Code_table _code_table1;
    private List<Proceduredetail> _proceduredetails;
    
    @Id
    @Column (name = "icdid")
    public Long getIcdid() {
        return _icdid;
    }
    public void setIcdid(Long icdid_) {
        _icdid = icdid_;
    }
    
    @Column (name = "cid")
    public Long getCid() {
        return _cid;
    }
    public void setCid(Long cid_) {
        _cid = cid_;
    }
    
    @Column (name = "icdcode")
    public String getIcdcode() {
        return _icdcode;
    }
    public void setIcdcode(String icdcode_) {
        _icdcode = icdcode_;
    }
    
    @Column (name = "status")
    public String getStatus() {
        return _status;
    }
    public void setStatus(String status_) {
        _status = status_;
    }
    
    @Column (name = "create_date")
    public String getCreate_date() {
        return _create_date;
    }
    public void setCreate_date(String createDate_) {
        _create_date = createDate_;
    }
    
    @Column (name = "kstatus")
    public String getKstatus() {
        return _kstatus;
    }
    public void setKstatus(String kstatus_) {
        _kstatus = kstatus_;
    }
    
    @Column (name = "enterorder")
    public int getEnterorder() {
        return _enterorder;
    }
    public void setEnterorder(int enterorder_) {
        _enterorder = enterorder_;
    }
    
    @Column (name = "poa")
    public String getPoa() {
        return _poa;
    }
    public void setPoa(String poa_) {
        _poa = poa_;
    }
    
    @Column (name = "ppoa")
    public String getPpoa() {
        return _ppoa;
    }
    public void setPpoa(String ppoa_) {
        _ppoa = ppoa_;
    }
    
    @Column (name = "userid")
    public String getUserid() {
        return _userid;
    }
    public void setUserid(String userid_) {
        _userid = userid_;
    }
    
    @ManyToOne
    @JoinColumn(name = "cid", insertable=false, updatable=false)
    public Code_table getCode_table() {
        return _code_table1;
    }
    public void setCode_table(Code_table code_table_) {
        _code_table1 = code_table_;
    }
    
    @OneToMany (mappedBy = "icdtable", targetEntity = Proceduredetail.class, cascade =  CascadeType.ALL)
    public List<Proceduredetail> getProceduredetails() {
        return _proceduredetails;
    }
    public void setProceduredetails(List<Proceduredetail> proceduredetails_) {
        _proceduredetails = proceduredetails_;
    }
    

    【讨论】:

    • 所以 Hibernate 无论如何都在执行连接?为什么会那样做,我不想那样做,因为我预计它会降低性能。如何防止它执行隐式连接?
    • 延迟加载在我看来是该问题的答案
    猜你喜欢
    • 2015-06-02
    • 2012-02-17
    • 2014-01-14
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多