簡體   English   中英

Spring Data Rest-如何建立關聯關系

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

我有兩個類, RolePermission ,它們之間具有ManyToMany關系。 我的問題是每個關系都附帶有一些額外的數據,因此我相信我需要創建一個中介類來存儲這些額外的數據,因此就是RolePermission類。

這基本上就是我所擁有的, parameterdomain是每種關系所需的額外數據。

在此處輸入圖片說明

這是我現在為班級准備的代碼。

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

@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

@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

每個班級都有一個像這樣的標准存儲庫

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

}

現在,此代碼可以很好地讀取數據和關系,但我的問題是我無法弄清楚該做什么或要調用哪個端點來添加/修改/刪除RolePermission之間的關系。

目前,如果我打電話給/roles/11/permissions我會找回這個:

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

如何為該角色添加權限?

我嘗試使用以下JSON正文對/roles/11/permissions執行POST請求,但得到了204 No Content響應。 這基本上意味着成功,但是當我對/roles/11/permissions進行GET請求時,那里沒有看到ID為1許可,因此它不起作用。

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

由於映射本身就是一個Entity ,因此可以基於Resource(在本例中為RolePermission )對API進行建模。 基本上,當您想提供API以添加rolepermission

就像是

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