Multiple Repositories for the Same Entity in Spring Data Rest

后端 未结 4 806
执念已碎
执念已碎 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:26

    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 {
    
        ...
    
    }
    
    
    public interface LineItemWithId extends Repository {
    
        ...
    
    }
    

    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?

提交回复
热议问题