使用 Springframework Page 对象时,不向前端公开具有复合主键的实体的路径 [英] Not exposing the path of entities that have a composite primary key to the front end when using the Springframework Page object
问题描述
我正在研究一个返回 Springframework 页面响应的 API 端点.我希望前端能够对数据进行排序,但我不能指望前端知道他们要排序的列实际上在复合主键内.
I'm working on an API endpoint that returns a Springframework Page response. I want the front end to be able to sort the data but I can't expect the front end to know that the column they want to sort on is actually inside a composite primary key.
在下面的示例(我正在处理的简化版本)中,您可以看到 startDate
列位于 RouteEntityPk
类中,该类链接到带有 @EmbeddedId
注释的 RouteEntity
类.要对该列进行排序,前端需要将 ?sort=pk.startdate,asc
添加到请求中.我希望前端只需要提供 ?sort=startdate,asc
.
In the example below (a simplified version of what I'm working on) you can see that the startDate
column is inside a RouteEntityPk
class, which is linked to the RouteEntity
class with the @EmbeddedId
annotation. To Sort on that column the front end would need to add ?sort=pk.startdate,asc
to the request. I want the front end to only have to provide ?sort=startdate,asc
.
有没有办法 - 使用 Spring 魔法 - 让存储库知道 startdate
== pk.startdate
,或者我是否必须编写一个翻译器来删除pk
向前端显示排序列时,从请求中读取时在需要的地方添加?
Is there a way - using Spring magic - of having the repository know that startdate
== pk.startdate
, or will I have to write a translator which will remove the pk
when showing the sort column to the front end, and add it where necessary when reading it from the request?
控制器:
@GetMapping(value = "routes/{routeId}", produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Page<Route>> getRouteByRouteId(@PathVariable(value = "routeId") final String routeId,
@PageableDefault(size = 20) @SortDefault.SortDefaults({
@SortDefault(sort = "order", direction = Sort.Direction.DESC),
@SortDefault(sort = "endDate", direction = Sort.Direction.DESC)
}) final Pageable pageable) {
return ResponseEntity.ok(routeService.getRouteByRouteId(routeId, pageable));
}
服务:
public Page<Route> getRouteByRouteId(String routeId, Pageable pageable) {
Page<RouteEntity> routeEntities = routeRepository.findByRouteId(routeId, pageable);
return new PageImpl<>(
Collections.singletonList(routeTransformer.toRoute(routeId, routeEntities)),
pageable,
routeEntities.getContent().size()
);
}
存储库:
@Repository
public interface RouteRepository extends JpaRepository<RouteEntity, RouteEntityPk> {
@Query(value = " SELECT re FROM RouteEntity re"
+ " AND re.pk.routeId = :routeId")
Page<RouteEntity> findByRouteId(@Param("routeId") final String routeId,
Pageable pageable);
}
实体:
路线:
@Data
@Entity
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "ROUTE", schema = "NAV")
public class RouteEntity {
@EmbeddedId
private RouteEntityPk pk;
@Column(name = "NAME")
private String name;
@Column(name = "ORDER")
private Integer order;
@Column(name = "END_DTE")
private LocalDate endDate;
}
RoutePk:
@Data
@Builder(toBuilder = true)
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class RouteEntityPk implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "ROUTE_ID")
private String routeId;
@Column(name = "STRT_DTE")
private LocalDate startDate;
}
模型:
路线:
@Data
@Builder
public class Route {
public String name;
public String routeId;
public List<RouteItem> items;
}
项目:
@Data
@Builder
public class Item {
public Integer order;
public LocalDate startDate;
public LocalDate endDate;
}
变压器:
public Route toRoute(String routeId, Page<RouteEntity> routeEntities) {
return Route.builder()
.name(getRouteName(routeEntities))
.routeId(routeId)
.items(routeEntities.getContent().stream()
.map(this::toRouteItem)
.collect(Collectors.toList()))
.build();
}
private Item toRouteItem(RouteEntity item) {
return ParcelshopDrop.builder()
.order(item.getOrder())
.startDate(item.getStartDate())
.endDate(item.getEndDate())
.build();
}
推荐答案
所以看起来这样做的方法是使用另一种处理 JPA 中复合主键的方法,注释 @IdClass代码>.通过这种方式,您可以将字段放在主实体中并以此引用它们.
So it looks like the way to do this is to use the other way you can deal with composite primary key's in JPA, the annotation @IdClass
. This way you can put the fields in the main entity and refer to them as such.
下面是我关注的 baeldung 文章的链接,以及对我在上面发布的实体所做的更改,使这项工作得以实现:
Below is a link to the baeldung article I followed and the changes to the entities I posted above that make this work:
https://www.baeldung.com/jpa-composite-primary-keys
实体:
路线:
@Data
@Entity
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@IdClass(RouteEntityPk.class)
@Table(name = "ROUTE", schema = "NAV")
public class RouteEntity {
@Id
@Column(name = "ROUTE_ID")
private String routeId;
@Id
@Column(name = "STRT_DTE")
private LocalDate startDate;
@Column(name = "NAME")
private String name;
@Column(name = "ORDER")
private Integer order;
@Column(name = "END_DTE")
private LocalDate endDate;
}
RoutePk:
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class RouteEntityPk implements Serializable {
private static final long serialVersionUID = 1L;
private String routeId;
private LocalDate startDate;
}
这篇关于使用 Springframework Page 对象时,不向前端公开具有复合主键的实体的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!