简体   繁体   中英

JPA: Select any row by distinct column

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

The solution to this problem was rather simple. First, we have to add the criteria for the multi select columns we seek, and set distinct to true:

final CriteriaBuilder builder = em.getCriteriaBuilder();
final CriteriaQuery<MyEntity> criteriaQuery = builder.createQuery(MyEntity.class);
final Root<MyEntity> myEntity= criteriaQuery.from(MyEntity.class);
final CriteriaQuery<MyEntity> select = criteriaQuery.multiselect(
                myEntity.get("zone"), versionTrack.get("school"));
select.distinct(true);

Then, we have to create an additional constructor for the MyEntity class so that it can be created from the results:

public MyEntity(final String zone, final String school) {...}

这样的东西行不通?

select min(a.zone), a.school from MyEntity a group by a.school

I am also facing the same issue. But read many site got an idea to solve the issue, now working fine. JPA Constructor doesn't allow the ' distinct ', so I try outside the constructor. Anyone found a better solution could you post the comment.

Code :

@Query("select distinct new com.apptium.eportalcdcdaas.model.CDCField(dl.fieldname,dl.fieldDataType,dl.fieldDesc,"
            + "dl.fieldPath,dl.parentdomainid,dl.primaryKey) from Delta dl where dl.accountName = ?1 and dl.applicationName = ?2"
            + " and dl.domainid = ?3 and dl.subDomainid = ?4")
    List getFieldWithDomainAndSubDomain(String accountname, String applicationname, String domainId,
            String subDomainId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM