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.
-
3This is not possible. With Spring Data REST a managed resource is an entity not a repository. The library maintains a `Map` of managed resources where the key is the entity class. Therefore, the entity can be mapped to only one repository interface at a time (since a `Map` can hold only one value for a key). If it is critical for your application to have multiple repositories per entity class, you may want to raise an enhancement request with the Spring Data team. – manish Mar 22 '16 at 02:57
-
There are MultiMaps. Technically there is surely no hurdle. From a semantical point of view, in our use cases, a resource can not be identified with an entity but rather with a view, in the sense of a DB. Views correspond to Projections in Spring Data Rest. So it would be great if I could map Projections and operations on them to resources. – Gregor Mar 22 '16 at 06:00
-
My comment was based on the [actual Spring Data REST implementation](https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ResourceMappingConfiguration.java). I know that there are structures like `MultiMap`s, which is why I suggested that you consider raising an enhancement request with the Spring Data team. – manish Mar 22 '16 at 06:31
-
To your comment, a table and its views are separate objects to me. If you have to execute DDL in order to fire DML queries, that becomes a separate object. If the database treats a table and a view separately, so should other application layers. With that in mind, a table will be mapped to its own JPA entity and a view to its own separate one. Both can then have their own repository interfaces and the existing Spring Data REST infrastructure will work just fine. – manish Mar 22 '16 at 06:36
4 Answers
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.

- 283
- 2
- 9
-
3I created a Jira Issue for this: https://jira.spring.io/browse/DATAREST-923 feel free to vote it up! – Tim Oct 18 '16 at 08:16
-
-
@Tim: We have similar requirements of needing two different repositories (one pointing to MySQL master instance and another pointing to slave instance with read-only access). All user based transaction should interact with our master repo. Batch frameworks (only perform reads) should interact with slave instance. – Chinmay Jan 30 '18 at 00:51
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.

- 833
- 7
- 8
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?

- 598
- 6
- 17
-
I tried this, but Hibernate seems not happy with it: org.hibernate.tool.schema.spi.SchemaManagementException: SQL strings added more than once for: line_item – Cyril Gambis Jul 25 '19 at 09:48