我正在使用 hibernate 进行简单的查询。没有连接。我想做的就是从表中检索最大 id。这项服务几个月来一直运行良好,但突然在过去两周内,我收到了可怕的 No row with the给定标识符存在错误。即使这个表包含数百万行。怎么会发生这种事?
这是进行查找的服务:
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_) {
}
}
最佳答案
即使您在没有连接的单个表上执行查询,查看查询中涉及的基础模型也很重要。如果该模型具有联接列并且关联的外键不存在于联接表中,则 hibernate 将失败。
在这种情况下,底层模型如下:有一个连接列 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_;
}
关于java - org.hibernate.ObjectNotFoundException : No row with the given identifier exists: Single table query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10002228/