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

4

3 回答 3

1

Do write

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

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

于 2013-04-25T09:00:14.380 回答
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

于 2013-04-25T09:02:28.157 回答
0

you could also try using Criteria.DISTINCT_ROOT_ENTITY

于 2013-04-25T09:21:24.457 回答