Hibernate 排序&关联查询。 200散分求助。 另开贴给分

meconsea 2006-04-25 01:16:40
目前有table1 &table2 两个表。table1的主键是table2的外键也是table2的主键。
table1 有字段 f1,f2,f3.
table2 有字段 z1,z2,z3. z1是table2的主键

我现在想实现按照 z3来排序 table1的内容。用sql我的写法是:
1、select * from table1 where f2= '1' oder by ( select z3 from table2 where table2.z1 = table1.f1)
2、seelct * from table1 t1,table2 t2 where t2.z1(+) = t1.f1 order by t2.z3.

两种写法都能在plsql执行。但是写成对应的hsql如下
1、from Table1 as table1 where table1.f2 = '1' order by (select table2.z3 from Table2 as table2 where table2.z1 = table1.f1)
执行结果报错是 缺少右括号。

2 方式取值出来我不知道对象是什么类型的。无法造型,既不是table1 也不是table2

求助,,,该问题如何处理。我用的hibernate2的版本。
...全文
747 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
caiway 2006-05-09
  • 打赏
  • 举报
回复
up
killme2008 2006-05-05
  • 打赏
  • 举报
回复
我不喜欢写SQL,所以我更喜欢Criteria和Query。楼主的数据库设计本身就有问题。
zhouzhenxin 2006-05-03
  • 打赏
  • 举报
回复
有点晕.
只看第一句就晕了.
为什么第二张表不设一下与业务无在的主键?
zclgod 2006-04-30
  • 打赏
  • 举报
回复
帮顶
hackangcn 2006-04-28
  • 打赏
  • 举报
回复
呵呵,什么是暗箱操作,除非特定的业务逻辑,一般项目都不用hql的
Saro 2006-04-27
  • 打赏
  • 举报
回复
个人认为,hql更加直观好用些,Criteria总是让我有种暗箱操作的感觉,不太喜欢,也许是sql写惯了的缘故吧....
hackangcn 2006-04-26
  • 打赏
  • 举报
回复
用HIBERNATE3吧,一个SetFetchMode加一个restrictions再来一个 addorder很方便的解决你问题,真正的面向对象,一个伪SQL语句都看不到
Ryo_Hazuki 2006-04-26
  • 打赏
  • 举报
回复
数据库设计的越好越能体现HIBERNATE的效用
雄牛 2006-04-26
  • 打赏
  • 举报
回复


无条件路过...
无条件回贴....
无条件接分....



Saro 2006-04-26
  • 打赏
  • 举报
回复
<class name="Table2" table="Table2">
<id name="id" column="z1">
<generator class="foreign">
<param name="property">table1</param>
</generator>
</id>
<property name="z2 />
<property name="z3 />
<one-to-one name="table1" class="Table1" constrained="true"/>
</class>
Saro 2006-04-26
  • 打赏
  • 举报
回复
你这必须作one-to-one的主键关联,
所以,至少类Table2必须有属性 Table1 table1,并在hbm文件中作出配置。

hql应如下:
select t1 from Table2 t2 right join t2.table1 t1 order by t2.z3
良少 2006-04-25
  • 打赏
  • 举报
回复
你的语法不对! HQL根本不是SQL语法, 它需要的元素,都是对象。
idilent 2006-04-25
  • 打赏
  • 举报
回复
直接order by object1.object2.z3就可以吧
object 1 是class1的实例class1对应table1
object2 是class2的实例class2对应table2
object2是object1的一个属性。
sole_lodestar 2006-04-25
  • 打赏
  • 举报
回复
from Table1 as table1 where table1.f2 = '1' order by (select table2.z3 from Table2 as table2 where table2.z1 = table1.f1)
是否应为
select table2.z3 from Table2 as table2,Table1 as table3 where table2.z1 = table3.f1

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧