2

我正在使用hibernate做一个简单的查询。没有连接。我要做的就是从表中检索最大 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_) {
        }
    }
4

1 回答 1

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_;
}
于 2012-04-04T02:05:13.447 回答