简体   繁体   English

Spring Boot 中 Hibernate/JPA 中的投影以从多个 OneToMany 和 manyToOne 关系中提取数据

[英]Projection in Hibernate/JPA in Spring Boot to pull data from multiple OneToMany and manyToOne relationships

I need to create a JPA projection that will pull all the records from Entity1 and include a record count from Entity4 .我需要创建一个JPA投影,将拉一切从记录Entity1 ,包括从记录计数Entity4

Here are the four entities that are mapped and their relationships:以下是映射的四个实体及其关系:

Entity1 (ManyToOne) Entity2 (OneToMany) Entity3 (OneToMany) Entity4 Entity1 (ManyToOne) Entity2 (OneToMany) Entity3 (OneToMany) Entity4

In SQL, this could easily be solved with a simple inline select statement.在 SQL 中,这可以通过简单的内联 select 语句轻松解决。 In the JPA projection, I am trying to do the following.在 JPA 投影中,我正在尝试执行以下操作。

@Value("#{target.getEntity2().getEntity3().getEntity4().size()}")
String  getEntity4Count();  

In this code above, the projection is on Entity1.在上面的这段代码中,投影在 Entity1 上。 It throws the exceptions when I add in Entity4.当我添加 Entity4 时它会抛出异常。 The below code works.下面的代码有效。

@Value("#{target.getEntity2().getEntity3().size()}")
String  getEntity3Count();

Is this even the correct method to pull this data?这甚至是提取这些数据的正确方法吗? All the examples seemed to point to relationships between two entities which is great for really simple applications but the SQL in business applications tends to be very complex.所有示例似乎都指向两个实体之间的关系,这对于非常简单的应用程序非常有用,但业务应用程序中的 SQL 往往非常复杂。

Is there a way to complete the above with another method such as JPQL?有没有办法用JPQL等另一种方法来完成上面的? What is the best method for handling large numbers of complex SQL that need to be executed about a set of Entities?处理需要针对一组实体执行的大量复杂 SQL 的最佳方法是什么?

Spring Data Projections is not really made for this kind of mapping as it will fetch entities and execute your "mappings" in the @Value annotation as SPEL(Spring Expression Language) code. Spring Data Projections 并不是真正为这种映射制作的,因为它会获取实体并在@Value注释中作为 SPEL(Spring 表达式语言)代码执行您的“映射”。 I suppose you would rather like to use more efficient SQL?我想您更愿意使用更高效的 SQL?

This is a perfect use case forBlaze-Persistence Entity Views .这是Blaze-Persistence Entity Views的完美用例。

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 模型和自定义接口或抽象类定义的模型之间轻松映射,例如类固醇上的 Spring Data Projections。 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 表达式将属性(getter)映射到实体模型。

A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

@EntityView(Entity1.class)
public interface Entity1Dto {
    @IdMapping
    Long getId();
    @Mapping("COUNT(entity2.entity3.entity4)")
    Long getCount();
}

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

Entity1Dto a = entityViewManager.find(entityManager, Entity1Dto.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 Data 集成允许您像使用 Spring Data Projections 一样使用它: 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