操作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] ]
我不知道这样的语句怎么写?请大家帮帮忙!
先谢了。