gpt4 book ai didi

java - 组织.hibernate.ObjectNotFoundException : No row with the given identifier exists: Single table query

转载 作者:行者123 更新时间:2023-11-30 09:38:44 27 4
gpt4 key购买 nike

我正在使用 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_) {
}
}

最佳答案

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

在这种情况下,底层模型如下:有一个连接列 cid 映射到另一个表 code_table。但是在icdtable中查询的值在code_table中没有对应的条目。即使查询仅在 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 - 组织.hibernate.ObjectNotFoundException : No row with the given identifier exists: Single table query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10002228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com