【问题标题】:Hibernate distinct clause problemHibernate distinct子句问题
【发布时间】:2011-05-07 15:42:49
【问题描述】:

我搜索了很多,但找不到答案:

我有以下 SQL 查询:

select distinct l.id_book from wa2011.tb_lending as l where l.id_user = 1

这是一个非常简单的查询,但我无法编写它。我怎样才能用 HQL 写这个?

非常感谢! 干杯。

【问题讨论】:

  • 我之前看过那篇文章,但我不知道“...inner join foo.bars ...” foo.bars 部分是对象还是什么...
  • 你答对了吗?

标签: sql hibernate hql distinct


【解决方案1】:

我用 Hibernate 3.3.2 和 MS SQL Server 测试了代码,它运行良好:

select distinct u.id from User u where u.login='admin'

所以我认为您的 HQL 代码应该看起来几乎相同(只需将其从 SQL 重写为映射到 Hibernate 的对象模型)。

【讨论】:

  • 感谢您的回答。问题是我想要唯一的字段实际上是另一个对象。如果我将子句“distinct x.bookBean bla bla bla”放入,结果将是 BookBean 对象的列表,而不是“from”子句中的对象列表。
  • 经过多对一映射测试,一切正常。你得到了哪个例外?
猜你喜欢
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
相关资源
最近更新 更多