简体   繁体   中英

JPA Criteria API - null check on GROUP BY

I need to return array of results, where data on the grouped column could contain null values, and those are skipped currently, while I would like to also have them grouped.

My entities:

public class UserEntity {
// ...
    @Basic
    @Column(name = "username")
    private String username;
}

public class ZgloszenieEntity {
// ... 

    @ManyToOne
    @JoinColumn(name = "assigned_user_id" )
    @OrderBy("username")
    private UserEntity assignedUser;

    @ManyToOne(targetEntity = InternalStatusEntity.class)
    @JoinColumn(name = "internal_status")
    @NotAudited
    private InternalStatusEntity internalStatus;
}

Code affected:

  CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<EfficiencyStatusReportDTO> cq = cb.createQuery(EfficiencyStatusReportDTO.class);
        Root<ZgloszenieEntity> root = cq.from(ZgloszenieEntity.class);
        Join<UserEntity, ZgloszenieEntity> join = root.join("assignedUser");

        cq.multiselect(join.get("username")
                , root.get("internalStatus").get("description")
                , getNumberOfDocOfType(cb, root, "X-1")
                , getNumberOfDOcOfType(cb, root, "X-2")
                , getNumberOfDocOfType(cb, root, "X-3")
                , getNumberOfDocOfType(cb, root, "X-4")
                , cb.count(root)
        );
        cq.groupBy(join.get("username"), root.get("internalStatus").get("description"));

It works fine for entities, that have internalStatus != null, but it is possible to have it without connected InternalStatus, and I would like to have it grouped by null then.

Current example result:

// ... 
        {
            "username": "admin@gmail.com",
            "internalStatus": "Do stuff",
            "numberOfDoc1": 0,
            "numberOfDoc2": 2,
            "numberOfDoc3": 1,
            "numberOfDoc4": 0,
            "sumOfDoc": 3
        },

And I would like to have it also result with something like this:

        {
            "username": "admin@gmail.com",
            "internalStatus": null,
            "numberOfDoc1": 4,
            "numberOfDoc2": 1,
            "numberOfDoc3": 5,
            "numberOfDoc4": 0,
            "sumOfDoc": 10
        },

Query generated by the JPA:

select userentity1_.username                                          as col_0_0_,
       internalst2_.description                                       as col_1_0_,
       count(case when zgloszenie0_.form_type='X-1' then 1 else null end) as col_2_0_,
       count(case when zgloszenie0_.form_type='X-2' then 1 else null end) as col_3_0_,
       count(case when zgloszenie0_.form_type='X-3' then 1 else null end) as col_4_0_,
       count(case when zgloszenie0_.form_type='X-4' then 1 else null end) as col_5_0_,
       count(zgloszenie0_.uid)                                        as col_6_0_
from zgloszenie zgloszenie0_
         inner join user userentity1_ on zgloszenie0_.assigned_user_id = userentity1_.id
         cross join internal_status internalst2_
where zgloszenie0_.internal_status = internalst2_.id
  and 1 = 1
group by userentity1_.username, internalst2_.description
order by userentity1_.username desc

Is there a way to change the cross join created automatically to have a null taken into the consideration?

Try this

 CriteriaBuilder cb = em.getCriteriaBuilder();
 CriteriaQuery<EfficiencyStatusReportDTO> cq = 
     cb.createQuery(EfficiencyStatusReportDTO.class);
 Root<ZgloszenieEntity> root = cq.from(ZgloszenieEntity.class);
 Join<ZgloszenieEntity, UserEntity> assignedUser = root.join("assignedUser", 
     JoinType.LEFT);
 Join<ZgloszenieEntity, InternalStatus> internalStatus = root.join("internalStatus", 
     JoinType.LEFT);

 cq.multiselect(assignedUser.get("username"), 
                internalStatus.get("description"),
                getNumberOfDocOfType(cb, root, "X-1"),
                getNumberOfDOcOfType(cb, root, "X-2"),
                getNumberOfDocOfType(cb, root, "X-3"),
                getNumberOfDocOfType(cb, root, "X-4"),
                cb.count(root)
 );
 cq.groupBy(assignedUser.get("username"), internalStatus.get("description"));

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