0

I am trying to write a distinct criteria query, using:

 CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
 CriteriaQuery<CorporateNews> query = criteriaBuilder.createQuery(CorporateNews.class);
 Root<CorporateNews> root = query.from(CorporateNews.class);
 query.select(root).distinct(true);
 return (List<CorporateNews>) entityManager.createQuery(query).getResultList();

However, this method is not working. The data further contain duplicates. Anyone know how to fix it?

I call out this method later in the REST API. I get the result:

[{"category":"Cinema"},{"category":"Cinema"},{"category":"Music"}]

However, I would get without repetition of data and so something like this:

[{"category":"Cinema"},{"category":"Music"}]
  • 1
    I think we need more information: what does your data looks like ? What was the expected output ? Thank you for editing. – BrunoLevy Nov 13 '15 at 09:25
  • 1
    and what was the generated SQL... – Neil Stockton Nov 13 '15 at 09:30
  • 1
    How to get generated SQL ? I do not really understand –  Nov 13 '15 at 09:59
  • 1
    you use a JPA implementation (only you know which one it is), and it has a log file, which can include the SQL when you configure the log to do so. aka step #1 in debugging your JPA code – Neil Stockton Nov 13 '15 at 10:06
  • 1
    See [How to view the SQL queries issued by JPA](http://stackoverflow.com/questions/4362876/how-to-view-the-sql-queries-issued-by-jpa) and post the entity definition and the data contained vs query output as suggested in the other comments – perissf Nov 13 '15 at 10:06
  • 1
    Apart from sql please post your `CorporateNews` entity and if you have some transformation logic in place in your Rest endpoint, please post that too. Is the `category` attribute unique for your entity? In general how would you define a "distinct" news item? Based on what attributes? How do your entries in DB look like? Please update the question and we will move from there. – yntelectual Nov 13 '15 at 20:30

0 Answers0