Spring Data Pageable 带有嵌套属性的排序

huangapple go评论65阅读模式
英文:

Spring Data Pageable sort with nested property

问题

在我的Spring Boot应用程序中我有一个具有嵌入式主键的实体

```java
public class MyEntity {
    
    @EmbeddedId
    private MyPk pk;

}

@Embeddable
public class MyPk implements Serializable {
    
    private Integer key1;

    private Integer key2;

}

要使用Spring的Pageable对象对结果进行排序,我必须调用REST服务:localhost?sort=pk.key1,asc

使用JPA,是否可以接受没有pk的请求?例如:localhost?sort=key1,asc

我希望能够在服务级别实现这一点。


<details>
<summary>英文:</summary>

in my Spring boot application I have an entity with an embedded primary key:

```java
public class MyEntity {
    
    @EmbeddedId
    private MyPk pk;

}

@Embeddable
public class MyPk implements Serializable {
    
    private Integer key1;

    private Integer key2;

}

To sort the results using spring's Pageable object I have to call the rest service at: localhost?sort=pk.key1,asc

Is it possible, using JPA, to accept requests without pk? For example: localhost?sort=key1,asc

I'd like to be able to do this at the service level

答案1

得分: 1

你需要在将它们传递给数据层之前重新编写这些排序部分:

@RequiredArgsConstructor
@Service
public class YourService {

    public static final Set&lt;String&gt; NESTED_PK_PROPERTIES = Set.of(&quot;key1&quot;, &quot;key2&quot;);

    private final YourRepository repository;

    @Transactional(readOnly = true)
    public Page&lt;YourResultType&gt; search(final Pageable pageable) {
        // 如果排序属性引用嵌套属性,则更正排序
        final Sort sort = Sort.by(
                pageable.getSort().get()
                        .map(order -&gt; NESTED_PK_PROPERTIES.contains(order.getProperty()) ?
                                Sort.Order
                                        .by(String.format(&quot;%s_%s&quot;, &quot;pk&quot;, order.getProperty()))
                                        .with(order.getDirection()) :
                                order)
                        .collect(Collectors.toList()));

        // 获取与搜索条件匹配的手动更改
        return repository.findAll(PageRequest.of(
                pageable.getPageNumber(), pageable.getPageSize(), sort));
    }

}
英文:

You would have to rewrite the sorts before passing them to the data layer:

@RequiredArgsConstructor
@Service
public class YourService {

    public static final Set&lt;String&gt; NESTED_PK_PROPERTIES = Set.of(&quot;key1&quot;, &quot;key2&quot;);

    private final YourRepository repository;

    @Transactional(readOnly = true)
    public Page&lt;YourResultType&gt; search(final Pageable pageable) {
        // Correct sort properties if they refer to a nested property
        final Sort sort = Sort.by(
                pageable.getSort().get()
                        .map(order -&gt; NESTED_PK_PROPERTIES.contains(order.getProperty()) ?
                                Sort.Order
                                        .by(String.format(&quot;%s_%s&quot;, &quot;pk&quot;, order.getProperty()))
                                        .with(order.getDirection()) :
                                order)
                        .collect(Collectors.toList()));

        // Get manual changes matching the search criteria
        return repository.findAll(PageRequest.of(
                pageable.getPageNumber(), pageable.getPageSize(), sort));
    }

}

huangapple
  • 本文由 发表于 2023年4月13日 22:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定