Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
apachecamel-Swagger-使用JPA实体作为rest类型_Jpa_Apache Camel_Swagger_Swagger Ui - Fatal编程技术网

apachecamel-Swagger-使用JPA实体作为rest类型

apachecamel-Swagger-使用JPA实体作为rest类型,jpa,apache-camel,swagger,swagger-ui,Jpa,Apache Camel,Swagger,Swagger Ui,我有一个剩余的DSL: // 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; }

我有一个剩余的DSL:

// 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
id\u角色
参数来创建(发布)新用户。我希望身体的例子显示如下:

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

我意识到我的实体没有被正确地创建。这些是我学到的:

  • @实体
    公共类用户{
    @身份证
    私有字符串id;
    @许多酮
    @JoinColumn(name=“id\u角色”)
    私人角色;
    }
    @实体
    公共阶级角色{
    @身份证
    私有字符串id;
    @OneToMany(mappedBy=“user”,cascade=CascadeType.ALL)
    私人名单用户;
    }
    
  • 要使类不是递归的,请设置@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;
    }
    
    @实体
    @JsonIdentityInfo(
    生成器=ObjectedGenerators.PropertyGenerator.class,
    property=“id”)
    公共类用户{
    @身份证
    私有字符串id;
    @许多酮
    @JoinColumn(name=“id\u角色”)
    私人角色;
    }
    @实体
    @JsonIdentityInfo(
    生成器=ObjectedGenerators.PropertyGenerator.class,
    property=“id”)
    公共阶级角色{
    @身份证
    私有字符串id;
    @OneToMany(mappedBy=“user”,cascade=CascadeType.ALL)
    @杰索尼奥雷
    //此属性不会出现在角色类中
    私人名单用户;
    }
    

  • 您可能需要向模型类添加招摇过市注释,因为它可能不理解JPA注释。
    @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;
    }