Multiple Repositories for the Same Entity in Spring Data Rest

后端 未结 4 805
执念已碎
执念已碎 2020-12-05 17:55

Is it possible to publish two different repositories for the same JPA entity with Spring Data Rest? I gave the two repositories different paths and rel-names, but only one o

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 18:14

    I ended up using the @Subselect to create a second immutable entity and bound that to the second JpaRepsotory and setting it to @RestResource(exported = false), that also encourages a separation of concerns.

    Employee Example

    @Entity
    @Table(name = "employee")
    public class Employee {
    
        @Id
        Long id
    
        String name
    
        ...
    
    }
    
    @RestResource
    public interface EmployeeRepository extends PagingAndSortingRepository {
    
    }
    
    @Entity
    @Immutable   
    @Subselect(value = 'select id, name, salary from employee')
    public class VEmployeeSummary {
    
        @Id
        Long id
    
        ...
    
    }
    
    @RestResource(exported = false)
    public interface VEmployeeRepository extends JpaRepository {
    
    }
    

    Context

    Two packages in the monolithic application had different requirements. One needed to expose the entities for the UI in a PagingAndSortingRepository including CRUD functions. The other was for an aggregating backend report component without paging but with sorting.

    I know I could have filtered the results from the PagingAndSorting Repository after requesting Pageable.unpaged() but I just wanted a Basic JPA repository which returned List for some filters.

提交回复
热议问题