我已经搜索了相当的解决方案,但仍然没有解决方案
@Entity
@Table(name = "documents")
public class Documents {
@Id
@Column(name = "doc_id", unique = true, nullable = false)
private String id;
@Column(name = "type")
private String type;
@Column(name = "url")
private String url;
}
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "address_id", unique = true, nullable = false)
private Integer addressId
@OneToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "address_docs", joinColumns = @JoinColumn(name = "address_id"), inverseJoinColumns = @JoinColumn(name = "doc_id"))
private List<Documents> doc;
}
public class Grd {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "grd_id", unique = true, nullable = false)
private Integer grdId
@OneToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "grd_docs", joinColumns = @JoinColumn(name = "grd_id"), inverseJoinColumns = @JoinColumn(name = "doc_id"))
private List<Documents> doc;
}
public class Person {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "grd_id", referencedColumnName = "grd_id")
private Grd grd;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "address_id")
private Address address;
}
当我将所有数据放入Person对象并保存对象时,它会给我错误
org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session
根据我的解决方案,我发现我已经将CascadeType改为MERGE,用于类Address和Grd。但现在我收到了错误
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [fk_address_doc_2]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我该如何做到这一点?