0

I want to get distinct values by a parameter:

@Transactional
public List<data> getAllFromColumn(String identifier) {

    List<data> resultList = em.createQuery("SELECT DISTINCT p.market FROM data p", Data.class).getResultList();

    return resultList;
}

My problem is that this only returns me a NullPointerException. Any recommendations what is wrong, or what I can do differently?

I appreciate your answer!

Jens
  • 67,715
  • 15
  • 98
  • 113
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 1
    Your `em` is set? Are you sure the query is the problem? – Jens Jul 23 '14 at 12:10
  • @Jens EntityManager works fine. Have proved that by doing other easier queries. – Carol.Kar Jul 23 '14 at 12:11
  • 1
    [http://stackoverflow.com/questions/300491/how-to-get-distinct-results-in-hibernate-with-joins-and-row-based-limiting-pagi] this may help you – subodh Jul 23 '14 at 12:13
  • your code mixes `data` and `Data`. Maybe just a tipo, but I think it is worth fixing it. – SJuan76 Jul 23 '14 at 12:14
  • Also, JPQL does support 'DISTINCT' – SJuan76 Jul 23 '14 at 12:15
  • 1
    Is this the entire code snippet? From the snippet, only em being null can result into NPE, getResultList is guaranteed to never be null. Stacktrace would help here? – maress Jul 23 '14 at 12:17
  • @maress Just checked it again! Yep thats the codesnipped. The thing is when loading hibernate with spring I get the NullPointerException. – Carol.Kar Jul 23 '14 at 12:22

1 Answers1

1

If HQL doesn't support select distinct (which it doesn't seem to according to the syntax), you can do this using group by:

SELECT p.market
FROM data p
GROUP BY p.market;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Just read sth about a criteria builder. Any recommendations for putting that into the criteria builder? What is the syntax? I appreciate your reply! – Carol.Kar Jul 23 '14 at 12:10
  • 1
    @Vivien . . . My expertise is in SQL, not Hibernate. In this case, the query can be turned into an equivalent valid query using `group by`. – Gordon Linoff Jul 23 '14 at 12:16