Multiple Repositories for the Same Entity in Spring Data Rest

╄→гoц情女王★ 提交于 2019-11-29 01:10:29

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.

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

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!