Consider the annotated POJO:
Code:
@Entity
@Table(name = "USERCASEDETAILS")
public class UserCaseDetails implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToOne
@Cascade( value = {org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private User user;
@ManyToOne
@Cascade( value = {org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Case caseInfo;
@ManyToOne
@Cascade( value = {org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Role role;
//etc etc
}
... resulting in the following SQL table schema:
Code:
mysql> describe usercasedetails;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| caseInfo_ID | bigint(20) | YES | MUL | NULL | |
| role_ID | bigint(20) | YES | MUL | NULL | |
| user_ID | bigint(20) | YES | MUL | NULL | |
+-------------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
The issue is this doesn't enforce uniqueness based upon the User, Role and Case . I'm not sure how to use the "unique" attribute in this scenario. Can someone tell me how to do this with annotations? A smal snippet or link would be extremely helpful. Bascially, I need to figure out how to specify that the combination of case+role+user is unique.
Thanks in advance
Code:
mysql> select * from usercasedetails;
+----+-------------+---------+---------+
| ID | caseInfo_ID | role_ID | user_ID |
+----+-------------+---------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | 1 |
| 3 | 3 | 1 | 1 |
| 4 | 4 | 2 | 2 |
| 5 | 5 | 2 | 2 |
| 6 | 6 | 2 | 2 |
| 7 | 7 | 3 | 3 |========>same
| 8 | 7 | 3 | 3 |========>same
| 9 | 7 | 3 | 3 |========>same
+----+-------------+---------+---------+
9 rows in set (0.00 sec)