简体   繁体   English

Spring Data Rest-如何建立关联关系

[英]Spring Data Rest - How to have an association relationship

I have two classes, Role and Permission with a ManyToMany relationship between them. 我有两个类, RolePermission ,它们之间具有ManyToMany关系。 My problem is that each relationship has some extra data that comes with it therefore I believe I need to create an intermediary class to store these extra data, so that is the RolePermission class. 我的问题是每个关系都附带有一些额外的数据,因此我相信我需要创建一个中介类来存储这些额外的数据,因此就是RolePermission类。

This is basically what I have, the parameter and domain are the extra data that are required for each relationship. 这基本上就是我所拥有的, parameterdomain是每种关系所需的额外数据。

在此处输入图片说明

Here is the code I have right now for my classes. 这是我现在为班级准备的代码。

Role.java Role.java

@Entity
@Table(name = "Sec_Role")
@DynamicUpdate
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String description;
    private String name;

    // This is another relationship which is working just fine (because there are no intermediary data needed.
    @ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER) 
    private Set<Group> groups;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "role")
    private List<RolePermission> permissions = new ArrayList<RolePermission>(0);

    ...Standard getters and setters and constructor

Permission.java Permission.java

@Entity
@Table(name = "Sec_Permission")
public class Permission {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "permission")
    private List<RolePermission> roles = new ArrayList<RolePermission>(0);

    ...Standard getters and setters and constructor

RolePermission.java RolePermission.java

@Entity
@Table(name = "Sec_Role_Permission")
@DynamicUpdate
public class RolePermission {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int domain;
    private String parameter;
    @Column(updatable = false, insertable = false)
    private int role_id;
    @Column(updatable = false, insertable = false)
    private int permission_id;

    @ManyToOne(fetch = FetchType.LAZY )
    @JoinColumn(name = "role_id", nullable = false)
    private Role role;

    @ManyToOne(fetch = FetchType.LAZY )
    @JoinColumn(name = "permission_id", nullable = false)
    private Permission permission;

    ...Standard getters and setters and constructor

Every class has a standard Repository like so 每个班级都有一个像这样的标准存储库

@RepositoryRestResource(path = "roles")
public interface RoleRepository extends CrudRepository<Role, Integer> {

}

Now this code works fine for reading data and relationships, my problem is that I cannot figure out what I am suppose to do or what end point to call to add/modify/delete a relationship between Role and Permission . 现在,此代码可以很好地读取数据和关系,但我的问题是我无法弄清楚该做什么或要调用哪个端点来添加/修改/删除RolePermission之间的关系。

Currently if I call /roles/11/permissions I will get this back: 目前,如果我打电话给/roles/11/permissions我会找回这个:

{
  "_embedded": {
    "rolePermissions": []
  },
  "_links": {
    "self": {
      "href": "http://localhost:8887/api/v1/roles/11/permissions"
    }
  }
}

How can I add a permission to this role? 如何为该角色添加权限?

I tried executing a POST request to /roles/11/permissions with the following JSON body and I got a 204 No Content response. 我尝试使用以下JSON正文对/roles/11/permissions执行POST请求,但得到了204 No Content响应。 This basically means success but then when I do a GET request to /roles/11/permissions I do not see permission with ID 1 there so it did not work. 这基本上意味着成功,但是当我对/roles/11/permissions进行GET请求时,那里没有看到ID为1许可,因此它不起作用。

{
  "domain": 0,
  "parameter": "Some param",
  "role_id": 11,
  "permission_id": 1
}

Since your mapping is itself an Entity you can model your API based on the Resource ( in this case RolePermission ). 由于映射本身就是一个Entity ,因此可以基于Resource(在本例中为RolePermission )对API进行建模。 Basically when you would want to provide an API to add rolepermission 基本上,当您想提供API以添加rolepermission

Some thing like 就像是

http://localhost:8887/api/v1/rolepermission

POST 

{
 "roleid":"xxxxx"
"permissionid":"xxxxxx"
"parameterid":"xxxxxx"
"domain":"xxxxx"

}

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

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