有关系的Spring POST请求

时间:2018-01-19 10:43:00

标签: spring hibernate

我在Spring中有两种实体类型,它们有一种关系:

@Entity
public class Domain {

  public Domain() {}

  @Id
  @GeneratedValue(strategy= GenerationType.IDENTITY)
  @Column(name = "id")
  private Integer id;
  private String name;
  private String description;
  private String image;

  @OneToMany(cascade=CascadeType.ALL, targetEntity=Subdomain.class,fetch = FetchType.LAZY)
  @JoinColumn(name="domain_id")
  private Set<Subdomain> subdomain = new HashSet<>();

  //Default getters and setters

  }

类型子域名:

@Entity
public class Subdomain {

  public Subdomain() {}

  @Id
  @GeneratedValue(strategy= GenerationType.IDENTITY)
  @Column(name = "id")
  private Integer id;
  private String name;

  @JsonIgnore
  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "domain_id", nullable = false)
  public Domain domain;

  //Default getters and setters
}

这与get请求完美配合,获取关系。但它如何与邮政请求一起使用?我将创建一个与现有域关系的新子域:

"domain_id": "2"

而且:

"domain_id": "http://localhost/subdomain/2"

但这不起作用。解决这个问题的最佳方法是什么?

Could not execute statement; SQL [n/a]; constraint [null]; nested       exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

0 个答案:

没有答案