操作hibernate多主键的问题?

各位大虾:
现在有个关于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] ]

我不知道这样的语句怎么写?请大家帮帮忙!
先谢了。

hbm文件再贴一次:
<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>

不知道你的ResultPK中,是不是已经覆盖重写了hashcode()和equals()方法?而且你的ResultPK一定要serializaion才行。

composite-id好像不能出现在Query语句中。

session.load(Result.class,resultPK);

谢谢,终于有人回复了:
我得ResultPK实现了implements Serializable,并且也实现了hashCode()
和equals(Object other)方法.
load()时,按照你的写法操作没问题,save()也可以。

现在是这样,cutomerId和setDate是双主键。
记录是这样:
customerID setDate
0001 2003-03-01 ........
0002 2003-03-01 .........

0001 2003-08-01 .......
0002 2003-08-01 .........

现在就是,在Query是,我要查所有customerId="0001" 的所有记录,
按照上面的记录,应该是两条,load()只能是一条。
comp_id是不能出现在hql语句中,但是我不知道怎么写,昨天,抠了一天,也没搞出来!

请你试试这样看如何,我觉得应可以了。(就仅把ResultPK当做Result中的一个属性)

from Result result
where result.comp_id .getSetDate = 'xxxx' and result.comp_id .getCustomerId='xxx'

只查日期就把and及其后面的删掉。

这应该不用试了,肯定不行。

from Result result where result.comp_id .customerId=:customerId

yehs220,谢谢,谢谢!!!

我太高兴了,终于弄好了。真的谢谢你!
另外我在chiaxp上面,也得到了回复。就是如果遇到多主键的情况,就在数据库表中再定义一列,作为主键(自动增加值),但是没有实际业务意义,只是标示唯一行。其他列正常定义。这样就避免了使用单独的主键类。

http://199.243.248.196/forum/viewThread.go?parentId=1057584016691&forum=1

只是在chinaxp我提的问题,不用主键类可以实现,如果要用到主键类,
还是出现错误。请大家帮帮忙,看一下。


chinaxp.org不错,推荐大家有空去逛逛:)