I have a SQL table, that contains some of this data:
------------------+------------------------+------------------------
Name + Zone + School
-------------------------------------------+------------------------
Joe + Coolzone + VCU
Mike + ReallyCoolZone + ODU
Kyle + NotcoolZone + ODU
What I am attempting to do here is using JPA to select just one row, with zone and school, where school is distinct.
Thus, I expect results:
CoolZone, VCU
ReallyCoolZone, ODU
I have tried to issue a query like this in JPA, but I get an error stating that more than one result was returned:
select distinct a.zone, a.school from MyEntity a
This error makes sense based on what I have read for two reasons. a) When we issue the select statement we are not returning the entire entity, rather portions b) jpa does not know how to map those values to my entity.
From what I have read, I can solve this issue by creating a new constructor and explicitly stating how the returned object should be constructed. This is mentioned in this post here: https://stackoverflow.com/a/24710759/5655414
After I create a new constructor, my query string equates to something like this:
select new MyEntity(distinct a.zone, a.school) from MyEntity a
I get an error stating:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct
At what location should this be placed? If this code is lunatic and makes no sense, is there possibly a more efficient way to achieve my goal?
JPA 2.0 Hibernate 3.4