要建立的关系:所有者拥有0个或更多角色的用户。 表格
Owner table -> Owner Information
User table -> User Information
Role table -> Role Information
OWNER_USER_LINK table -> Owner to user relationship with a unique id
OWNER_USER_LINK_ROLE table -> each entry in OWNER_USER_LINK can have zero or more entries in OWNER_USER_LINK_ROLE for different roles.
Owner Class
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "OWNER_USER_LINK", joinColumns = {
@JoinColumn(name = "OWNER_ID", referencedColumnName = "OWNER_ID") }, inverseJoinColumns = {
@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") })
private Set<OwnerUserRoles> ownerUserRoles = new HashSet<>();
OwnerUserRoles类
@Entity
@Table(name = "OWNER_USER_LINK")
@JsonIgnoreProperties(ignoreUnknown = true)
public class OwnerUserRoles {
@Id
@Column(name = "ROW_ID")
@GeneratedValue
private Long id;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "USER_ID")
private User user;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "OWNER_ID")
private Owner owner;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "OWNER_USER_LINK_ROLE", joinColumns = {
@JoinColumn(name = "ROW_ID", referencedColumnName =
"ROW_ID") })
Set<Role> roles;
}
我收到以下异常,嵌套的异常是 org.hibernate.MappingException:外键 (FK6ajkqol5o4m1dtnbnmvvsy3er:owner_user_link_role [row_id]))必须 具有与引用的主键相同的列数 (owner_user_link [owner_id,user_id])
如何在休眠状态下与JPA建立这种关系?任何帮助或指针,我们将不胜感激。