Hibernate重复映射字段

有时候,使用@ManyToMany@JoinColumn这种形式的表关联会和原本的@Basic@Column造成冲突

代码如下:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Role.class)
@JoinColumn(name="role_id")
public Role getRole() {
    return role;
}

@Basic
@Column(name = "role_id", nullable = true)
public Integer getRoleId() {
    return roleId;
}

解决方法
在@JoinColumn中将属性设置只读

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Role.class)
@JoinColumn(name="role_id", insertable = false, updatable = false)
public Role getRole() {
    return role;
}

相关推荐