JiveJdon Community Forums
在线478人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 8 回复 / 1
 发表新帖子   回复该主题贴
zhengzhiyong

悄悄话
发表文章: 18
注册时间: 2002年12月12日 10:03
操作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] ]

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

悄悄话
发表文章: 18
注册时间: 2002年12月12日 10:03
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>
bruce

悄悄话
发表文章: 191
注册时间: 2003年05月28日 09:53
Re: 操作hibernate多主键的问题? 2003年08月25日 09:53 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
不知道你的ResultPK中,是不是已经覆盖重写了hashcode()和equals()方法?而且你的ResultPK一定要serializaion才行。
yehs220

悄悄话
发表文章: 101
注册时间: 2003年01月04日 12:50
Re: 操作hibernate多主键的问题? 2003年08月25日 10:14 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
composite-id好像不能出现在Query语句中。

session.load(Result.class,resultPK);
zhengzhiyong

悄悄话
发表文章: 18
注册时间: 2002年12月12日 10:03
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语句中,但是我不知道怎么写,昨天,抠了一天,也没搞出来!





bruce

悄悄话
发表文章: 191
注册时间: 2003年05月28日 09:53
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及其后面的删掉。
yehs220

悄悄话
发表文章: 101
注册时间: 2003年01月04日 12:50
Re: 操作hibernate多主键的问题? 2003年08月25日 11:47 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
这应该不用试了,肯定不行。
yehs220

悄悄话
发表文章: 101
注册时间: 2003年01月04日 12:50
Re: 操作hibernate多主键的问题? 2003年08月25日 11:50 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
from Result result where result.comp_id .customerId=:customerId
zhengzhiyong

悄悄话
发表文章: 18
注册时间: 2002年12月12日 10:03
Re: 操作hibernate多主键的问题? 2003年08月25日 12:35 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
yehs220,谢谢,谢谢!!!

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

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

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


chinaxp.org不错,推荐大家有空去逛逛:)
这个主题有 8 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  每2分种自动备份发贴内容Ctrl-V粘贴取出,提问题前先查询标签列表

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com

anti spam