gpt4 book ai didi

java - JPQL 按子列计算多个多对一和组计数

转载 作者:行者123 更新时间:2023-12-04 17:25:41 26 4
gpt4 key购买 nike

enter image description here

我想构建一个 JPQL 查询来将该结构的数据映射到这个 DTO:

@AllArgsConstructor
class UserDTO {
long userId;
long countOfContacts;
Map<String,Long> countOfActions; // by type
}

我不知道如何在 JPQL 中提取每个 Action 类型的计数,这就是我被困的地方(看到我的名字?:)):

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();
}

我使用 postgres,如果这在 JPQL 中不可能,也可以使用 native 查询。

实际上,我可以通过 native 查询并在 Java 中映射操作数据来解决它,但感觉很糟糕:

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
;

产生这样的结果数据:

  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 | {}
...

由于在 native 查询中我们无法映射到 POJO,因此我返回 List<String[]>并自己将所有列转换为 UserDTO的构造函数:

@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;
}

然后我映射countOfActions到 Java Map<String, Long>在 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);
}
}
}
}

我可以在 DB 层解决它吗?

最佳答案

不幸的是JPQL没有像 string_agg 这样的聚合函数或 group_concat .所以你应该自己转换查询结果。首先,您应该像这样创建一个“普通”查询,例如:

@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();

(它是 HQL - JPQL 的 Hibernate 扩展)

此查询的结果将是一个普通表,例如:

|--------|---------------|-------------|-------------|
|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 |
|--------|---------------|-------------|-------------|

然后您应该将该结果分组到 UserDto 的集合中,像这样:

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();
}

然后瞧,你会在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:

@Value
class UserPlainDto {
long userId;
long contactCount;
ActionType actionType;
long actionCount;
}

@Data
class UserDto {
long userId;
long contactCount;
Map<ActionType, Long> actions = new HashMap<>();
}

My demo project .

关于java - JPQL 按子列计算多个多对一和组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com