2012-04-03 14 views
2

我正在做一個使用hibernate的簡單查詢。沒有聯接。我所要做的就是從表中檢索最大的ID。這項服務在幾個月內運行良好,但突然間,在最近兩週內,我得到了帶有給定標識符存在錯誤的可怕No行。即使此表包含數百萬行。這怎麼可能發生?org.hibernate.ObjectNotFoundException:不存在具有給定標識符的行:單表查詢

這裏是一個確實的查找服務:

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

回答

2

即使你是在沒有一個單一的表進行查詢的連接,也要看參與的基本模式是很重要的查詢。如果該模型具有連接列,並且關聯的外鍵不在連接表中,則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_; 
} 
+0

10因此,Hibernate是否正在執行連接?爲什麼會這樣做,我不想這樣做,因爲我預計它會降低性能。我如何防止它執行隱式連接? – Jonah 2014-03-03 21:01:42

+0

延遲加載在我看來就是這個問題的答案 – kenny 2014-05-12 12:57:34

相關問題