org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:单表查询

时间:2012-04-03 22:15:08

标签: java sql-server hibernate

我正在使用hibernate进行简单的查询。没有连接。我想要做的就是从表中检索最大id。这项服务运行了好几个月,但突然间,在过去的两周内,我得到了可怕标识符存在错误的可怕行。即使此表包含数百万行。怎么会发生这种情况?

以下是查找服务:

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_) {
        }
    }

1 个答案:

答案 0 :(得分:2)

即使您在没有连接的单个表上进行查询,也必须查看查询中涉及的基础模型。如果该模型具有连接列,并且连接表中不存在关联的外键,则休眠将失败。

在这种情况下,底层模型如下所示:有一个连接列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_;
}