操作hibernate多主鍵的問題?

zhengzhiyong發表於2003-08-25
各位大蝦:
現在有個關於hibernate多主鍵的問題,請指教:
一個表Result,有兩個主鍵,分別是customerId,setDate,用Middlegen Hibernate plugin生成一個單獨的主鍵類ResultPK.

下面是從資料庫生成的result.hbm.xml檔案:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.persistent.Result" table="result"
>
<composite-id name="comp_id" class="com.persistent.ResultPK">
<key-property name="customerId" column="customerID" type="java.lang.String" />
<key-property name="setDate" column="setDate" type="java.lang.String" />
</composite-id>
<property
name="degree"
type="int"
column="degree"
length="3"
/>
<property
name="prize"
type="java.lang.String"
column="prize"
not-null="true"
length="2"
/>
<property
name="wage"
type="int"
column="wage"
not-null="true"
length="5"
/>
<property
name="train"
type="int"
column="train"
not-null="true"
length="2"
/>

<!-- associations -->

</class>
</hibernate-mapping>



現在我想查詢表中所有customerId='0001' and setDate='2003-08-12' 的記錄,怎麼寫HQL語句???,急死我了!
我寫的HQL語句:

//首先給主鍵類賦值
ResultPK resultPK = new ResultPK(); //結算主鍵表
resultPK.setCustomerId("0001");
resultPK.setSetDate("2003-08-12");


//注:comp_id是Result類中ResultPK的例項
//在Result類中有 public ResultPK getComp_id() {}
// public void setComp_id(ResultPK comp_id) {} 方法。

下面是我寫的hql語句:
session.find("select result.comp_id from Result result where result.comp_id = "+resultPK+" ");

但是執行時出現錯誤:就在session.find(....)那行出錯:
net.sf.hibernate.QueryException: path expression ends in a composite value: result0_.comp_id [select result.comp_id from com.persistent.Result result where result.comp_id = com.persistent.ResultPK@11946c2[customerId=0001,setDate=2003-08-12] ]

我不知道這樣的語句怎麼寫?請大家幫幫忙!
先謝了。

相關文章