使用Springframework Page对象时,不向前端公开具有复合主键的实体的路径 [英] Not exposing the path of entities that have a composite primary key to the front end when using the Springframework Page object
问题描述
我正在使用一个API端点,该端点返回一个Springframework Page响应.我希望前端能够对数据进行排序,但是我不能指望前端知道他们要排序的列实际上在复合主键内部.
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屋!