 |
上一主题
数据库是ORACLE8.15,现在现象是好像一切都正常,日志也正常,可是数据库中始终没有数据,大大帮帮忙阿!
对应的JAVA文件和HBM如下
package com.sunix;
i..
|
|
下一主题
环境为WEBLOGIC7.0,JDK1.4.0.TEST环境为JUNIT,ANT.
执行时出现例外为:现在状态是什么都查询不出.我已苦闷了好几天,请大家帮帮忙,我会非常感谢的.
javax.e..
|
|
|
|
|
|
|
|
操作hibernate多主键的问题?
|
2003年08月25日 09:42
|
|
|
标签列表
|
|
各位大虾: 现在有个关于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] ]
我不知道这样的语句怎么写?请大家帮帮忙! 先谢了。
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 09:44
|
|
|
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>
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 09:53
|
|
|
|
不知道你的ResultPK中,是不是已经覆盖重写了hashcode()和equals()方法?而且你的ResultPK一定要serializaion才行。
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 10:14
|
|
|
composite-id好像不能出现在Query语句中。
session.load(Result.class,resultPK);
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 10:35
|
|
|
谢谢,终于有人回复了: 我得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语句中,但是我不知道怎么写,昨天,抠了一天,也没搞出来!
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 11:24
|
|
|
请你试试这样看如何,我觉得应可以了。(就仅把ResultPK当做Result中的一个属性)
from Result result where result.comp_id .getSetDate = 'xxxx' and result.comp_id .getCustomerId='xxx'
只查日期就把and及其后面的删掉。
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 11:47
|
|
|
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 11:50
|
|
|
|
from Result result where result.comp_id .customerId=:customerId
|
|
|
|
|
|
Re: 操作hibernate多主键的问题?
|
2003年08月25日 12:35
|
|
|
|
|
|
|
热点TAG:
AOP
cache
缓存
DDD
EJB
集群
设计模式
Hibernate
IOC
JiveJdon
OO
RBAC
Seam
Spring
Struts
anti spam
|