简体   繁体   English

如何使用JPA map native query into nested projection

[英]How to Use JPA map native query into nested projection

I have two tables, User and Roles, they are many-to-many我有两个表,用户和角色,它们是多对多的

Also they are large tables, don't expect all irrelevant column returns.它们也是大表,不要期望所有不相关的列返回。

So I have native query, (and have to use native).所以我有本机查询,(并且必须使用本机)。

select u.name, r.name, r.uuid
from user u
       join user_role_join urj on urj.user_uuid = u.uuid
       join role r on r.uuid = urj.role_uuid;

From return, one user show multiple roles.从返回开始,一个用户显示多个角色。

Adam | superAdmin | {uuid1}
Adam | admin      | {uuid2}
Lisa | guest      | {uuid3}
...

I need make returns to fit with pojo我需要回报以适应 pojo

User
 String name,
 List<Role> roles;

Role
 String name,
 UUID uuid

I'm using EntityManager to do the query.我正在使用 EntityManager 进行查询。

How could I make JPA know map one user with multiple Roles?我怎样才能让 JPA 知道 map 一个具有多个角色的用户?

{
 name:Adam
 roles:[
 {
   uuid:{uuid1}
   name:superAdmin
 },
 {
   uuid:{uuid2}
   name:admin
 }
 ]
},
{
 name:Lisa
 roles:[
 {
   uuid:{uuid3}
   name:guest
 }
 ]
} 

I don't know why you need to use a native query, but when collections are involved, JPA/Hibernate can only help you when using entities.我不知道为什么你需要使用原生查询,但是当涉及到 collections 时,JPA/Hibernate 只能在使用实体时帮助你。

If you want avoid unnecessary select items, I can recommend you try to use a JPA model with Blaze-Persistence as this is a perfect use case forBlaze-Persistence Entity Views .如果您想避免不必要的 select 项,我建议您尝试使用 JPA model 和 Blaze-Persistence,因为这是Blaze-Persistence 实体视图的完美用例。

I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids.我创建了库以允许在 JPA 模型和自定义接口之间轻松映射或抽象 class 定义的模型,类似于类固醇上的 Spring 数据投影。 The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.这个想法是,您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将 map 属性(getter)定义为实体 model。

A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:对于您的用例,DTO model 对于 Blaze-Persistence 实体视图可能如下所示:

@EntityView(User.class)
public interface UserDto {
    @IdMapping
    UUID getId();
    String getName();
    Set<RoleDto> getRoles();

    @EntityView(Role.class)
    interface RoleDto {
        @IdMapping
        UUID getUuid();
        String getName();
    }
}

Querying is a matter of applying the entity view to a query, the simplest being just a query by id.查询是将实体视图应用于查询的问题,最简单的就是通过 id 进行查询。

UserDto a = entityViewManager.find(entityManager, UserDto.class, id);

The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features Spring 数据集成允许您几乎像 Spring 数据投影一样使用它: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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