简体   繁体   English

自定义Spring Data Rest @ManyToMany关系处理

[英]Customizing Spring Data Rest @ManyToMany relationship handling

I've got a simple project running Spring-Data-Rest to expose some entities via a Rest API, like this one (simplified, minus Setters/Getters): 我有一个运行Spring-Data-Rest的简单项目,通过Rest API公开一些实体,比如这个(简化,减去Setters / Getters):

@Entity 
public class Group {

    @Id
    @GeneratedValue
    private int id;

    ...

}

@Entity 
public class Person{

    @Id
    @GeneratedValue
    private int id;

    ...

}

Now, obviously every group can have Persons as members, which can be solved quite easily via @ManyToMany . 现在,显然每个小组都可以拥有人员作为成员,这可以通过@ManyToMany轻松解决。 Unfortunately (or typically?) a group membership contains more info than just "is member of". 不幸的是(或者通常是?)组成员资格包含的信息不仅仅是“是成员”。 For example, it should also include the information "is admin of" or "is hidden member of". 例如,它还应包括“是管理员”或“是隐藏成员”的信息。

This leads naturally to an extra entity... 这自然导致了一个额外的实体......

@Entity 
public class GroupMember {

    @OneToOne(optional = false)
    @JoinColumn(updatable = false)
    private Person member;

    @OneToOne(optional = false)
    @JoinColumn(updatable = false)
    private Group group;

    private boolean admin;

    private boolean hidden;

        ...

}

Which would not be that bad, but unfortunately leads to a problem... I can no longer simply add members to a group by POST ing an text/uri-list to (for example) /groups/1/members , but I have to instead create a new GroupMember entity by posting to /groupMembers/ which is, imho, not as comfortable anymore and breaks the cohesion of the Group REST 'tree'. 这不会是坏的,但不幸的是导致了问题...我不能再简单地通过添加成员到组POST荷兰国际集团的text/uri-list来(例如) /groups/1/members ,但我有而是通过发布到/groupMembers/来创建一个新的GroupMember实体,这是imho,不再那么舒服,并打破了群组REST'树'的凝聚力。

How can I solve this and allow a simple POST to create a (basic) GroupMember, with default values? 如何解决这个问题并允许简单的POST创建一个(基本的)GroupMember,默认值为? Of course I could put a new @RepositoryRestController there that catches any POST request to /groups/1/members , but this prevents the list of text/uri-list (since it doesn't seem to support that). 当然我可以在那里放一个新的@RepositoryRestController来捕获/groups/1/members任何POST请求,但这会阻止text / uri-list列表(因为它似乎不支持)。 I could define a new object, for example with a person id, but this would break the flow, simple posting a link list would be better. 我可以定义一个新对象,例如使用人员ID,但这会破坏流程,简单发布链接列表会更好。

Another way would be to find a way to map the Hibernate @ManyToMany itself without an extra entity... But I don't know such a way... 另一种方法是找到一种方法来映射Hibernate @ManyToMany本身而不需要额外的实体......但我不知道这样的方式......

If you are working with spring data rest and want to handle text/uri-list in a custom controller you can pass the value using a parameter of type Resources like this: 如果您正在处理spring数据休息并希望在自定义控制器中处理text/uri-list ,则可以使用类型为Resources的参数传递值,如下所示:

@RequestBody Resources<Object> incoming

You obtain the uris by calling incoming.getLinks() 你通过调用incoming.getLinks()获得uris

You can look at the spring data rest controller as reference - org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController#createPropertyReference 您可以将spring数据rest org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController#createPropertyReference作为参考 - org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController#createPropertyReference

I think you're right that it is best to have a third entity, GroupMember as you proposed. 我认为你最好有一个第三个实体,就像你提出的GroupMember Can't you still expose an API endpoint that lets you post to /groups/1/members ? 你还不能公开一个允许你发布到/groups/1/members的API端点吗? To do you it would seem that you'll need to have business logic in your controller that resolves the appropriate Person(s) and constructs GroupMember instances. 为了做到这一点,您似乎需要在控制器中使用业务逻辑来解析相应的Person并构造GroupMember实例。 This seems to me to be the appropriate place for this business logic to reside. 在我看来,这是适合这种业务逻辑的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM