HSQL。如何使用Distinct排除重复元素

时间:2014-09-13 08:37:49

标签: java hibernate hsqldb

我在hibernate查询中有重复的结果,例如:

select new ValueObject(h.id, c.firstName, c.lastName) 
from HistoryTable as h left join CustomerTable as c
where h.customerId = c.id and c.notDeleted
order by c.firstName, c.lastName

但是,当我使用DISTINCT时,结果中的重复会继续显示

select distinct new ValueObject(h.id, c.firstName, c.lastName) 
from HistoryTable as h left join CustomerTable as c
where h.customerId = c.id and c.notDeleted
order by c.firstName, c.lastName

但我的问题是,是否有可能使用DISTINCT排除重复项以在HSQLDB查询中创建新的ValueObject?

1 个答案:

答案 0 :(得分:0)

对于具有左连接或右连接的查询,Hibernate不会返回不同的结果。 您可以将Hiberante setResultTransformer用于您的目的。有关更多细节的解释,解释原因和解决方法,请查看:

https://developer.jboss.org/wiki/HibernateFAQ-AdvancedProblems#jive_content_id_Hibernate_does_not_return_distinct_results_for_a_query_with_outer_join_fetching_enabled_for_a_collection_even_if_I_use_the_distinct_keyword

How do you create a Distinct query in HQL