2014-09-13 30 views
0

我没有在stackOverFlow中找到关于我的问题的答案。HSQL。如何使用区别排除dublicate元素

我有重复的结果在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的重复项?

回答

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

+0

谢谢你的事情与setResultTransformer。我不知道这个功能。但真正明显的是左外连接的工作,并且我在安置该职位后立即找到了错误原因。 – Aventes 2014-09-23 20:10:14