specs: hibernate 3.3.1.ga, anno 3.4.0.ga, em 3.4.0.ga
Hi,
First of all, I have read a lot of docs (maybe I missed something :( ).
Does referencedColumnName attribute of @JoinColumn need to refer only to an ID of an entity to generate a foreign key? What if I want to refer to a unique key but is not an ID (but a business key)?
Consider this example:
Code:
@Entity
public class User
{
@Id
private long id;
@Column(unique=true)
private String username
@ManyToMany
@JoinTable(
name = "user_role",
joinColumns={
@joinColumn(
name="username",
referencedColumnName="username")
},
inverseJoinColumns={
@JoinColumn(
name="roleId",
referencedColumnName="id")
},
uniqueConstraints={
@UniqueConstraint(
columnNames={"username","roleId"})
}
)
private List<Role> roles;
}
@Entity
public class Role
{
@Id
private long id;
@Column(unique=true)
private String roleName;
private String roleDescription;
@ManyToMany(mappedBy="roles")
private Collection<User> users = new ArrayList<User>();
}
Short explanation:
user_role is the bridge table where it refers a role by role's id and refers a user by username instead of user's id
The problem:
hibernate did not generate a foreign key constraint for the username field of user_role table as it would have if i used user's id instead of user's username.
And if I use roleName for the bridge table to refer to a role instead of role's id, then the bridge table would have no foreign key at all.
Thanks.