【发布时间】:2019-07-12 20:15:23
【问题描述】:
我正在使用带有 mySql db 的 hibernate 和 spring boot。 启用正在生成 sql 的 DDL 模式。 但不知道为什么要在每个更改表中添加 unique 约束。 对于我的大部分参考,我不想要唯一,但仍然是 hibernate 正在添加它。
@Entity
public class Users extends BaseEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Size(max = 100)
@NotNull
private String username;
@Size(max = 150)
@NotNull
private String password;
@Size(max = 100)
@NotNull
@Column(unique = true)
private String email;
@JoinColumn(name = "role_id",**unique=false**)
@OneToOne(fetch = FetchType.EAGER ,optional = false)
private Role role;
}
@Entity
public class Role implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "role_id",nullable = false)
private Integer roleId;
@Enumerated(EnumType.STRING)
@Size(max = 10)
@Column(name = "role_type",nullable = false,length = 10)
private RoleType roleType;
@Size(max = 50)
@Column(name = "description",length = 50)
private String description;
}
DDL by Hibernate -
alter table users
add constraint UK_pkddbcvou26qe5hsawu1mbnlb **unique** (role_id)
但我不想在我的用户表中出现 唯一 (role_id),因为可以将多个用户分配给一个角色。 是否由于一对一映射。有没有办法说不要在用户表中创建唯一的(role_id)。 即使我尝试使用 @JoinColumn(name = "role_id",unique=false) 但没有运气。任何人都可以在这里提供帮助。
【问题讨论】:
标签: mysql hibernate spring-boot