spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
906 stars 558 forks source link

DATAREST-1280 Add support sort collection resource by associated property. #2328

Open mikoto2000 opened 8 months ago

mikoto2000 commented 8 months ago

Closes #1641 Related tickets #1386 #1343

I delete those code and execute sort 4 patterns.

https://github.com/spring-projects/spring-data-rest/blob/f0e86b9a176bbaf43fbc3fba62e935c691e35222/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JacksonMappingAwareSortTranslator.java#L195-L197

Currently, it seems to support linkable associations. Therefore, I have removed the check process for linkable associations.

Are there any other patterns that should be tested further?

Patterns result:

eager and exported Path: `/accounts_eager_and_exported?sort=accountType.typeName,desc` Query: ``` Hibernate: select a1_0.id,a1_0.account_type_id,a1_0.name from account a1_0 left join account_type a2_0 on a2_0.id=a1_0.account_type_id order by a2_0.type_name desc offset ? rows fetch first ? rows only ```
Entity: AccountEagerAndExported.java ```java package dev.mikoto2000.study.springboot.data.rest.example.entity; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Data; import org.springframework.data.rest.core.annotation.RestResource; @Data @Entity @Table(name = "account") public class AccountEagerAndExported { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @RestResource(exported = true) @JoinColumn(name = "account_type_id") @ManyToOne(fetch = FetchType.EAGER) private AccountType accountType; } ```
Repository: AccountEagerAndExportedRepository.java ```java package dev.mikoto2000.study.springboot.data.rest.example.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import dev.mikoto2000.study.springboot.data.rest.example.entity.AccountEagerAndExported; @RepositoryRestResource(collectionResourceRel = "accounts_eager_and_exported", path = "accounts_eager_and_exported") public interface AccountEagerAndExportedRepository extends JpaRepository { } ```
eager and no exported Path: `/accounts_eager_and_no_exported?sort=accountType.typeName,desc` Query: ```sql Hibernate: select a1_0.id,a1_0.account_type_id,a1_0.name from account a1_0 left join account_type a2_0 on a2_0.id=a1_0.account_type_id order by a2_0.type_name desc offset ? rows fetch first ? rows only ```
Entity: AccountEagerAndNoExported.java ```java package dev.mikoto2000.study.springboot.data.rest.example.entity; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Data; import org.springframework.data.rest.core.annotation.RestResource; @Data @Entity @Table(name = "account") public class AccountEagerAndNoExported { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @RestResource(exported = false) @JoinColumn(name = "account_type_id") @ManyToOne(fetch = FetchType.EAGER) private AccountType accountType; } ```
Repository: AccountEagerAndNoExportedRepository.java ```java package dev.mikoto2000.study.springboot.data.rest.example.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import dev.mikoto2000.study.springboot.data.rest.example.entity.AccountEagerAndNoExported; @RepositoryRestResource(collectionResourceRel = "accounts_eager_and_no_exported", path = "accounts_eager_and_no_exported") public interface AccountEagerAndNoExportedRepository extends JpaRepository { } ```
lazy and exported Path: `accounts_lazy_and_exported?sort=accountType.typeName,desc` Query: ``` Hibernate: select a1_0.id,a1_0.account_type_id,a1_0.name from account a1_0 left join account_type a2_0 on a2_0.id=a1_0.account_type_id order by a2_0.type_name desc offset ? rows fetch first ? rows only ```
Entity: AccountLazyAndExported.java ```java package dev.mikoto2000.study.springboot.data.rest.example.entity; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.Data; import org.springframework.data.rest.core.annotation.RestResource; @Data @Entity @Table(name = "account") public class AccountLazyAndExported { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @RestResource(exported = true) @JoinColumn(name = "account_type_id") @ManyToOne(fetch = FetchType.LAZY) private AccountType accountType; } ```
Repository: AccountLazyAndExportedRepository.java ```java package dev.mikoto2000.study.springboot.data.rest.example.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import dev.mikoto2000.study.springboot.data.rest.example.entity.AccountLazyAndExported; @RepositoryRestResource(collectionResourceRel = "accounts_lazy_and_exported", path = "accounts_lazy_and_exported") public interface AccountLazyAndExportedRepository extends JpaRepository { } ```
lazy and no exported In the first place, it is not supported by Spring Data REST.

Example code repository: https://github.com/mikoto2000/spring-data-rest-example