Fetch List Using DTO projections using a Constructor Expression and JPQL

前端 未结 3 1168
無奈伤痛
無奈伤痛 2021-02-13 18:14

Perform a search on DisabScreenRequest and fetch its child details also. Using DTO projections using a Constructor Expression and JPQL.

The parent entity with a child

3条回答
  •  迷失自我
    2021-02-13 18:58

    This is a perfect use case for Blaze-Persistence Entity Views.

    I created the library to allow easy mapping between JPA models and custom interface defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure the way you like and map attributes(getters) via JPQL expressions to the entity model. Since the attribute name is used as default mapping, you mostly don't need explicit mappings as 80% of the use cases is to have DTOs that are a subset of the entity model.

    A mapping for your model could look as simple as the following

    @EntityView(DisabScreenRequest.class)
    interface RequestSearchDto extends Serializable {
      @IdMapping
      long getRequestId();
      Long getCivilId();
      Set getDisabilities();
    }
    

    Querying is a matter of applying the entity view to a query, the simplest being just a query by id.

    RequestSearchDtodto = entityViewManager.find(entityManager, RequestSearchDto.class, id);

    But the Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/1.4/entity-view/manual/en_US/#spring-data-features

提交回复
热议问题