问题描述
我正在做一个使用hibernate的简单查询。没有联接。我所要做的就是从表中检索最大的ID。这项服务在几个月内运行良好,但突然间,在最近两周内,我得到了带有给定标识符存在错误的可怕No行。即使此表包含数百万行。这是怎么发生的?
以下是查找服务:
try {
session = HibernateUtils.beginTransaction(mydatabase);
criteria = session.createCriteria(MyClass.class);
criteria.setMaxResults(1);
订单顺序= Order.desc(id);
criteria.addOrder(order);
myclass =(MyClass)criteria.uniqueResult();
} catch(HibernateException e_){
e_.printStackTrace();
String msg =从myclass表中获取最大id的问题
+ e_.getCause();
LOG.error(msg);
抛出新的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;
私人长_cid;
私人字符串_icdcode; // TODO:将它添加到hibernate映射('PN'或'NN')
private String _status;
私人字符串_create_date;
private String _kstatus;
private int _enterorder;
私人字符串_poa;
私人字符串_ppoa;
私人字符串_userid;
private Code_table _code_table1;
私人列表< 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_;
$ b $ @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_;
$ b @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_;
}
I am doing a simple query using hibernate. There are no joins. All I am trying to do is retrieve the maximum id from a table. This service was working well for months, but suddenly, within the last two weeks, I get the dreaded No row with the given identifier exists error. Even though this table contains millions of rows. How can this be happening?
Here is the service which does the look-up:
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_) {
}
}
Even if you are doing a query on a single table with no joins, it is important to look also at the underlying model involved in the query. If that model has a join column and the associated foreign key is not present in the joined table, hibernate will fail.
In this case the underlying model is as below: There is a join column cid that maps to another table code_table. But there was no corresponding entry in the code_table with the value being queried in the icdtable. Even though the query was only on the icdtable, the fact that the underlying model joins with another table requires that there be data integrity across the tables not just in the table on which the query is being performed.
@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_;
}
这篇关于org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:单个表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!