2

I would like to fetch a list from a query in Hibernate but without repeated elements.

Currently i have something like:

SELECT t FROM Table t join fetch t.list tl WHERE tl.userid=:userid AND tl.tableid=t.id

This works good! the problem its that it returns the same object as many times as userid its in tl

so lets say userid its found 3 times in tl i am getting:

T
  TL1
  TL2
  TL3
T
  TL1
  TL2
  TL3
T
  TL1
  TL2
  TL3

and i want to get:

T
  TL1
T
  TL2
T
  TL3

or just one:

T
  TL1
  TL2
  TL3

I guess its possible in Hibernate but havent manage it yet.

Thanks in advance

3 Answers 3

1

Do write

SELECT  distinct t FROM Table t join fetch t .......
      -----^------

HQL ORDER BY clause and DISTINCT clause will be helpful further.

Sign up to request clarification or add additional context in comments.

2 Comments

That was easy, dint know the distinct stament was possible that implicit. Thanks
Its not that much until we know it :) happens to every one glad to help you :)
0

Why you don't use then DISTINCT

  SELECT DISTINCT t FROM Table t join fetch t.list tl WHERE tl.userid=:userid 
AND tl.tableid=t.id

It will give you result without the repeated values

Comments

0

you could also try using Criteria.DISTINCT_ROOT_ENTITY

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.