![](/img/trans.png)
[英]How to return a custom object from a Spring Data JPA GROUP BY query
[英]Create Map object from GROUP BY in JPA Query
为简单起见,我有 3 个表:
公司有员工,每个员工都有许多分配给他们的设备。 我对连接到他们分配给员工的设备名称列表的 companyId 列表感兴趣。
示例 Java 对象:
@Getter
@Setter
@AllArgsConstructor
public class CompanyDevices {
private Long companyId;
private List<String> deviceNames;
}
或者甚至更好的 Map 会很棒:Map<Long, List 其中 Long 将是 companyId,List 将是 deviceNames;
甚至可以在 JPARepository 中使用 @Query 创建 JPA 查询、连接所有这些表并返回自定义对象列表或映射吗?
我现在所拥有的课程略有不同:
@Getter
@Setter
@AllArgsConstructor
public class CompanyDevice {
private Long companyId;
private String deviceName;
}
public interface SomeRepository extends JpaRepository<SomeEntity, Long> {
@Query("SELECT " +
"new path.to.CompanyDevice(C.id, D.deviceName) " +
"FROM Company C " +
"JOIN C.employees E " +
"JOIN E.devices D " +
"WHERE C.id IN :companyIds"
)
List<CompanyDevice> findDevicesForCompanies(@Param("companyIds") List<Long> companyIds);
}
因此,我需要稍后手动分组并创建地图。 所以我想做的是这样的事情,但我找不到足够的信息,或者甚至可能不可能(对不起,伪代码):
public interface SomeRepository extends JpaRepository<SomeEntity, Long> {
@Query("SELECT " +
"<create map where key is C.id and value is <list of D.deviceName from grouping>>" +
"FROM Company C " +
"JOIN C.employees E " +
"JOIN E.devices D " +
"WHERE C.id IN :companyIds" +
"GROUP BY C.id"
)
Map<Long, List<String>> findDevicesForCompanies(@Param("companyIds") List<Long> companyIds);
}
您需要的是这样的中间函数:
public interface SomeRepository extends JpaRepository<SomeEntity, Long> {
@Query("SELECT " +
"c.id, d.deviceName " +
"FROM Company c " +
"JOIN c.employees e " +
"JOIN e.devices d " +
"WHERE c.id IN :companyIds"
)
List<Object[]> findDevicesForCompanies0(@Param("companyIds") List<Long> companyIds);
default Map<Long, List<String>> findDevicesForCompanies(List<Long> companyIds) {
return findDevicesForCompanies(companyIds).stream()
.collect(
Collectors.groupingBy(
o -> (Long) o[0],
Collectors.mapping( o -> (String) o[1], Collectors.toList() )
)
);
}
}
解决这个问题的另一个好方法是使用Blaze-Persistence Entity Views ,我认为这是一个完美的用例。
我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义的模型之间轻松映射,例如类固醇上的 Spring Data Projections。 这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。
使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:
@EntityView(Company.class)
public interface CompanyDevices {
@IdMapping
Long getId();
@Mapping("employees.devices.deviceName")
Set<String> getDeviceNames();
}
查询是将实体视图应用于查询的问题,最简单的只是按 id 查询。
CompanyDevices a = entityViewManager.find(entityManager, CompanyDevices.class, id);
Spring Data 集成允许您几乎像 Spring Data Projections 一样使用它: https : //persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
Page<CompanyDevices> findAll(Pageable pageable);
或者在您的特定情况下
List<CompanyDevices> findByIdIn(List<Long> companyIds);
最好的部分是,它只会获取实际需要的状态!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.