Hibernate version: 3.2.2.ga
Name and version of the database you are using: MySQL 5.1
Hi All,
I am using the technique described on page 308 of "Java Persistence with Hibernate" to create a ternary association. That is, I have created an @Embeddable class with two @ManyToOne associations:
Code:
@Embeddable
public class UserGroupPermission {
// Constructor, getters/setters, and hashCode/equals removed for brevity
@Parent
private User user;
@ManyToOne
@JoinColumn(name = "groupId", nullable = false, updatable = false)
private Group group;
@ManyToOne
@JoinColumn(name = "permissionId", nullable = false, updatable = false)
private Permission permission;
}
In the parent class I created the collection of components:
Code:
@CollectionOfElements
@JoinTable(name = "UserGroupPermission", joinColumns = @JoinColumn(name = "userId"))
private List<UserGroupPermission> userGroupPermissions = new ArrayList<UserGroupPermission>();
This all works great and is almost exactly what I want except for one thing. The problem is that I need every row to be unique, ie the primary key should be all three columns of the association table. Hibernate creates a table that allows non unique combinations:
Code:
mysql> describe usergrouppermission;
+--------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| userId | bigint(20) | NO | MUL | NULL | |
| groupId | bigint(20) | NO | MUL | NULL | |
| permissionId | bigint(20) | NO | MUL | NULL | |
+--------------+------------+------+-----+---------+-------+
Is there any way to force hibernate to create the table such that all of the columns are part of the primary key?
Thanks!