我有三个表:用户、角色、董事会。每个用户可以拥有多个版块,但在一个版块中,用户只能拥有一个角色。我创建了三个类并添加了注释:
class User {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "mail")
private String mail;
...
@JoinTable(name = "userBoardRole",
joinColumns = @JoinColumn(name = "id", unique = false),
inverseJoinColumns = @JoinColumn(name = "role_id", unique = false))
@MapKeyJoinColumn(name = "board_id", unique = false)
@ElementCollection
private Map<Board, Role> boardRoleMap = new HashMap<Board, Role>();
}
class Role {
@Id
@GeneratedValue
@Column(name = "role_id")
private long id;
@Column(name = "name")
private String name;
...
}
class Board {
@Id
@GeneratedValue
@Column(name = "role_id")
private long id;
@Column(name = "name")
private String name;
...
}
Hibernate 创建一个新表,如下所示:
CREATE TABLE
userboardrole
(id
bigint(20) NOT NULL,role_id
bigint(20) NOT NULL,board_id
bigint(20) NOT NULL, PRIMARY KEY (id
,board_id
), UNIQUE KEYUK_3lunj2moakkbpehqwqkjcvlf4
(role_id
), KEYFK_3lunj2moakkbpehqwqkjcvlf4
(role_id
), KEYFK_ta3fwgh4sln85f6f3jjbte38u
(board_id
), KEYFK_lgd2b2mph9qoc1pe2h9r2wu8u
(id
), CONSTRAINTFK_lgd2b2mph9qoc1pe2h9r2wu8u
FOREIGN KEY (id
) 参考users
(id
), 约束外FK_3lunj2moakkbpehqwqkjcvlf4
键 (role_id
) 参考boardrole
(role_id
), 约束外FK_ta3fwgh4sln85f6f3jjbte38u
键 (board_id
) 参考boards
(board_id
)) ENGINE=InnoDB 默认字符集=utf8;
问题是列 role_id 是唯一的。有没有办法使用注释将唯一的列设置为false?感谢您的任何帮助。
我正在使用休眠 4.2.2。