【问题标题】:Hibernate new keyword with distinct使用不同的休眠新关键字
【发布时间】:2011-10-22 11:38:16
【问题描述】:

我需要使用当前的 hql:

select distinct a from Asset as a where ...

并将其更改为

select new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ...

我的问题在于 distinct 关键字。它属于使用新对象查询类型的 hql 查询中的什么位置。一个想法是使用子选择并在那里有我的独特之处。我已经尝试添加distinct a.id,但这不起作用。

【问题讨论】:

    标签: hibernate hql new-operator distinct


    【解决方案1】:

    任何有兴趣的人都可以,正确的语法是

    select distinct new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ...
    

    【讨论】:

    • 这是否意味着你的查询结果,不受 AssetDTO equals() 方法的影响?
    • 为什么查询结果会受到equals()方法的影响?也许我不明白你的问题
    • distinct new com.org.AssetDTO 等效于 distinct a from Asset,但不等效于 distinct(a.id, a.address, a.status)。实体的所有字段都用于选择DISTINCT?
    • select distinct new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a 与 select distinct a.id, a 的普通 sql select 语句相同.address, a.status 来自 Asset a,然后提供一个 RowMapper 将其转换为 DTO 数组,这种语法是/曾经是 HQL 等价物。其中
    • 但是如果我需要更多列然后我需要在 distinct 子句中怎么办?例如,如果我只想在 (a.id, a.address) 上有所不同,但我还需要 a.status 的数据
    猜你喜欢
    • 2014-07-02
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多