JPQL calculates multiple many-to-one and group counts by subcolumns
I want to build a JPQL query to map the data of that structure to this DTO:
@AllArgsConstructor
class UserDTO {
long userId;
long countOfContacts;
Map<String,Long> countOfActions; by type
}
I
don’t know how to extract the count of each Action type in JPQL, that’s where I’m stuck (see my name?). :)):
public interface UserRepository extends CrudRepository<User, Long> {
@Query("SELECT new example. UserDTO( "
+ " u.id, "
+ " COUNT(contacts), "
--> + " ??? group by actions.type to map<type,count>??? " <---
+ " ) "
+ " FROM User u "
+ " LEFT JOIN u.actions actions "
+ " LEFT JOIN u.contacts contacts "
+ " GROUP BY u.id")
List<UserDTO> getAll();
}
I use
postgres, and if that’s not possible in JPQL, I can also use native queries.
Actually, I can solve it with native queries and mapping operational data in Java, but it feels bad :
SELECT
u.id,
COALESCE(MIN(countOfContacts.count), 0) as countOfContacts,
ARRAY_TO_STRING(ARRAY_REMOVE(ARRAY_AGG(actions.type || ':' || actions.count), null),',') AS countOfActions
FROM user u
LEFT JOIN (
SELECT
user_id, COUNT(*) as count
FROM contact
GROUP BY user_id
) countOfContacts
ON countOfContacts.user_id = u.id
LEFT JOIN (
SELECT
user_id, type, COUNT(*)
FROM action
GROUP BY user_id, type
) actions
ON actions.user_id = u.id
GROUP BY u.id
;
Produce the result data like this:
id | countOfContacts | countOfActions
--------+-----------------+-------------------------
11728 | 0 | {RESTART:2}
9550 | 0 | {}
9520 | 0 | {CLEAR:1}
12513 | 0 | {RESTART:2}
10238 | 3 | {CLEAR:2,RESTART:5}
16531 | 0 | {CLEAR:1,RESTART:7}
9542 | 0 | {}
...
Since we can’t map to POJOs in native queries, I go back to List<String[]>
and convert all the columns to the constructor of UserDTO
myself:
@Query(/*...*/)
/** use getAllAsDTO for a typed result set */
List<String[]> getAll();
default List<UserDTO> getAllAsDTO() {
List<String[]> result = getAll();
List<UserDTO> transformed = new ArrayList<>(result.size());
for (String[] row : result) {
long userId = Long.parseLong(row[0]);
long countOfContacts = Long.parseLong(row[1]);
String countOfActions = row[2];
transformed.add(
new UserDTO(userId, countOfContacts, countOfActions)
);
}
return transformed;
}
Then I map countOfActions
to Java Map<String, Long>
in the constructor of the DTO:
class UserDTO {
long userId;
long countOfContacts;
Map<String,Long> countOfActions; by type
/**
* @param user
* @param countOfContacts
* @param countOfActions {A:1,B:4,C:2,..} will not include keys for 0
*/
public UserDTO(long userId, long countOfContacts, String countOfActionsStr) {
this.userId = userId;
this.countOfContacts = countOfContacts;
this.countOfActions = new HashMap<>();
remove curly braces
String s = countOfActionsStr.replaceAll("^\\{|\\}$", "");
if (s.length() > 0) { // exclude empty "arrays"
for (String item : s.split(",")) {
String[] tmp = item.split(":");
String action = tmp[0];
long count = Long.parseLong(tmp[1]);
countOfActions.put(action, count);
}
}
}
}
Can I work around it at the DB layer?
Solution
Unfortunately< a href="https://docs.oracle.com/html/E13946_05/ejb3_langref.html#ejb3_langref_aggregates" rel="noreferrer noopener nofollow">JPQL doesn’t look like string_agg such an aggregate function or group_concat So you should transform the query results yourself. First, you should create a “normal” query like this, for example:
@Query("select new example. UserPlainDto( " +
" a.user.id, " +
" count(distinct c.id), " +
" a.type, " +
" count(distinct a.id) " +
") " +
"from " +
" Action a " +
" join Contact c on c.user.id = a.user.id " +
"group by " +
" a.user.id, a.type")
List<UserPlainDto> getUserPlainDtos();
(It is HQL – JPQL.) Hibernate extension).
The result of this query will be a normal table, for example:
|--------|---------------|-------------|-------------|
|user_id |countact_count |action_type |action_count |
|--------|---------------|-------------|-------------|
|1 |3 | ONE |1 |
|1 |3 | TWO |2 |
|1 |3 | THREE |3 |
|2 |2 | ONE |1 |
|2 |2 | TWO |2 |
|3 |1 | ONE |1 |
|--------|---------------|-------------|-------------|
You should then group that result into a collection of UserDinto
, like this:
<pre class=”lang-java prettyprint-override”>default Collection<UserDto> getReport() {
Map<Long, UserDto> result = new HashMap<>();
getUserPlainDtos().forEach(dto -> {
long userId = dto.getUserId();
long actionCount = dto.getActionCount();
UserDto userDto = result.getOrDefault(userId, new UserDto());
userDto.setUserId(userId);
userDto.setContactCount(dto.getContactCount());
userDto.getActions().compute(dto.getActionType(), (type, count) -> count != null ? count + actionCount : actionCount);
result.put(userId, userDto);
});
return result.values();
}
Then voila, you’ll get this in Collection<UserDto>
:
[
{
"userId": 1,
"contactCount": 3,
"actions": {
"ONE": 1,
"TWO": 2,
"THREE": 3
}
},
{
"userId": 2,
"contactCount": 2,
"actions": {
"ONE": 1,
"TWO": 2
}
},
{
"userId": 3,
"contactCount": 1,
"actions": {
"ONE": 1
}
}
]
DTO:: is used above
@Value
class UserPlainDto {
long userId;
long contactCount;
ActionType actionType;
long actionCount;
}
@Data
class UserDto {
long userId;
long contactCount;
Map<ActionType, Long> actions = new HashMap<>();
}