2017-05-05 97 views
0

我有這樣的休息-DSL:Apache的駱駝揚鞭 - 使用JPA實體其餘類型

// this api creates new user 
rest("/user") 
    .post() 
    .type(User.class).to("jpa://com.project.User") 

這是我的實體:

public class User{ 
    @Id 
    private String id; 
    @ManyToOne 
    @JoinColumn(name = "id_role") 
    private Role role; 
} 

public class Role{ 
    @Id 
    private String id; 
    @OneToMany(mappedBy="user") 
    private List<User> users; 
} 

我的問題是在我的身體數值參數招搖例。它包含這樣的:

{ 
    "id": "string", 
    "role": { 
    "id": "string", 
    "users": [ 
     { 
     "id": "string", 
     "roles": [ 
      {} 
     ] 
     } 
    ] 
    } 
} 

相當複雜的,雖然我只需要idid_role參數來創建(POST)新用戶。我希望身體示例如下所示:

{ 
    "id": "string", 
    "id_role": "string" 
} 
+1

您可能需要招搖的註釋添加到您的模型類,因爲它可能不理解JPA註釋。 –

回答

1

我意識到我的實體沒有正確創建。這些是我瞭解到:

  1. Configure CascadeType in associated JPA entities

    @Entity 
    
    public class User { 
        @Id 
        private String id; 
        @ManyToOne 
        @JoinColumn(name = "id_role") 
        private Role role; 
    } 
    
    @Entity 
    public class Role{ 
        @Id 
        private String id; 
        @OneToMany(mappedBy="user", cascade = CascadeType.ALL) 
        private List<User> users; 
    } 
    
  2. 使類不是遞歸,設置@JsonIgnore

    @Entity 
    @JsonIdentityInfo(
         generator = ObjectIdGenerators.PropertyGenerator.class, 
         property = "id") 
    public class User{ 
        @Id 
        private String id; 
        @ManyToOne 
        @JoinColumn(name = "id_role") 
        private Role role; 
    } 
    
    @Entity 
    @JsonIdentityInfo(
         generator = ObjectIdGenerators.PropertyGenerator.class, 
         property = "id") 
    public class Role{ 
        @Id 
        private String id; 
        @OneToMany(mappedBy="user", cascade = CascadeType.ALL) 
        @JsonIgnore 
        // this attribute will not appear inside Role class 
        private List<User> users; 
    }