2012-02-28 107 views
1

我想使用@embeddable執行組合鍵查詢。休眠查詢組合鍵

這是我到目前爲止。

@Embeddable 
public class IfasvVendorPK implements Serializable{ 

@Column(length = 4, nullable = false) 
protected String peId; 
@Column(length = 8, nullable = false) 
protected String peAddrCd; 

實體

@Entity 
public class IfasvVendor implements Serializable { 

@EmbeddedId 
private IfasvVendorPK ifasvVendorPK; 

查詢

列表包含兩個PKS。不知道我是否應該使用這個列表。

  Query query = session.createQuery("from IfasvVendor t0 where  t0.ifasvVendorPK.peId=:id"); 
      query.setParameter("id", list); 
      query.list(); 

我也不知道如何獲取對象,一旦我得到查詢工作。

+0

這不是一個複合鍵查詢,而是通過組合鍵中的一個屬性進行搜索。請確認這是你正在努力完成的。無論如何,假設你想傳遞一個屬性列表,你當前的查詢是不正確的。使用此代替 - '從IfasvVendor t0,其中t0.ifasvVendorPK.peId in:idList'和'query.setParameter(「idList」,list)'。 – Perception 2012-02-28 16:55:03

回答

2

我認爲有以下應該工作:

Query query = session.createQuery("from IfasvVendor t0 where t0.ifasvVendorPK.peId in (:id)"); 
query.setParameterList("id", list); 

query.list(); 

則必須用括號中的命名參數在你的查詢,並使用setParameterList。查看javadoc的setParameterList here

查詢結果將在返回列表中:query.list()。這會返回一個未經檢查的列表,您可能想要將其轉換爲List<IfasvVendor>

btw。這不是一個複合鍵查詢。請參閱@Perception評論...

+0

我相信你和知覺是正確的。我沒有機會回到它嘗試修復,但它看起來非常有前途。 – 2012-02-29 19:01:20