Using Hibernate 3.2 Annotations and DDL to generate table I do I stop one of the primary key from been flagged as unique.
I have a scenario where my users might have more than one role. I have implemented this using JoinTable as shown below
@OneToMany
@JoinTable(name="UserRole",
joinColumns = { @JoinColumn( name="user_id", unique = false) },
inverseJoinColumns = @JoinColumn( name="role_id", unique=false, insertable=false, updatable=false)
)
public Set<Role> getRoles() {
return roles;
}
void setRoles(Set<Role> roles){
this.roles = roles;
}
public void addRole(Role role) {
getRoles().add(role);
}
The generated SQL from the DDL utility are shown below (NOTICE that role_id is flagged as unique)
create table UserRole (
user_id bigint not null,
role_id bigint not null,
primary key (user_id, role_id),
unique (role_id)
) type=InnoDB;
alter table UserRole
add index FKF3F76701F991D007 (user_id),
add constraint FKF3F76701F991D007
foreign key (user_id)
references User (id);
alter table UserRole
add index FKF3F7670154670C27 (role_id),
add constraint FKF3F7670154670C27
foreign key (role_id)
references Role (id);
|