Multiple Repositories for the Same Entity in Spring Data Rest

13,246

Solution 1

The terrible part is not only that you can only have 1 spring data rest repository (@RepositoryRestResource) per Entity but also that if you have a regular JPA @Repository (like CrudRepository or PagingAndSorting) it will also interact with the spring data rest one (as the key in the map is the Entity itself). Lost quite a few hours debugging random load of one or the other. I guess that if this is a hard limitation of spring data rest at least an Exception could be thrown if the key of the map is already there when trying to override the value.

Solution 2

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<Employee, Long> {

}
@Entity
@Immutable   
@Subselect(value = 'select id, name, salary from employee')
public class VEmployeeSummary {

    @Id
    Long id

    ...

}
@RestResource(exported = false)
public interface VEmployeeRepository extends JpaRepository<VEmployeeSummary, Long> {

}

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.

Solution 3

The answer seems to be: There is only one repository possible per entity.

Solution 4

So, this does not directly answer the question, but may help solve the underlying issue.

You can only have one repository per entity... however, you can have multiple entities per table; thus, having multiple repositories per table.

In a bit of code I wrote, I had to create two entities... one with an auto-generated id and another with a preset id, but both pointing to the same table:

@Entity
@Table("line_item")
public class LineItemWithAutoId {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    ...
}



@Entity
@Table("line_item")
public class LineItemWithPredefinedId {

    @Id
    private String id;

    ...
}

Then, I had a repository for each:

public interface LineItemWithoutId extends Repository<LineItemWithAutoId,String> {

    ...

}


public interface LineItemWithId extends Repository<LineItemWithPredefinedId,String> {

    ...

}

For the posted issue, you could have two entities. One would be the full entity, with getters and setters for everything. The other, would be the entity, where there are setters for everything, but only getters for the fields you want to make public. Does this make sense?

Share:
13,246
Gregor
Author by

Gregor

Updated on June 07, 2022

Comments

  • Gregor
    Gregor almost 2 years

    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 of the two is available as REST endpoint. The point why I'm having two repositories is, that one of them is an excerpt, showing only the basic fields of an entity.