2

I have simple (H)SQL query:

SELECT f 
FROM Factory f 
LEFT JOIN FETCH f.products p 
LEFT JOIN FETCH p.customers 
WHERE f.id = (:id)

But result contains multiple duplicated products objects.. How I can solve this problem?

Please note: customers and products is collections i.e. there one-to-many association between factory and product and between product and customer .

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • use SELECT DISTINCT ? – kostja Nov 12 '13 at 13:58
  • @kostja I think it will remove duplicates of Factory objects. But I need to remove duplicates of `products` – WelcomeTo Nov 12 '13 at 14:00
  • The query will return Factory objects with their attached Products objects (and their attached Customer objects). The current query will return duplicate Factory entities. If those Factory objects contain duplicate Product objects, you will get them all. I must be misunderstanding your question. – kostja Nov 12 '13 at 14:10
  • See here. Fetch does an inner-join and you must then filter the duplicates somehow. See here for an explanation and a comment suggesting using a result transformer to deal with the duplicates will not work for > 1 collection. http://stackoverflow.com/questions/1995080/hibernate-criteria-returns-children-multiple-times-with-fetchtype-eager – Alan Hay Nov 12 '13 at 16:07

1 Answers1

0

Here is my suggestion.

  1. Define Products attribute inside Factory as Set instead of List.
    • Factory -> Set< Product >
  2. Define Customers attribute inside Product as Set instead of List.
    • Product -> Set< Customer >
  3. Implemented equals & hashCode method for Customer & Product

This should be able to handle the duplicates.