-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How do I prevent duplicates in the following scenario?
PostPosted: Thu May 28, 2009 10:35 am 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
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)


Top
 Profile  
 
 Post subject: Re: How do I prevent duplicates in the following scenario?
PostPosted: Thu May 28, 2009 1:00 pm 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
Here's the solution I figured out: use a unique constraint

Code:
@Entity
@Table(name = "USERCASEDETAILS", uniqueConstraints = { @UniqueConstraint(columnNames = {"USER_ID" ,"CASEINFO_ID", "ROLE_ID"}) })
public class UserCaseDetails implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   @Id
   @PrimaryKeyJoinColumn
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;

   @ManyToOne
   @Cascade(value = { org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE })
   @PrimaryKeyJoinColumn (name = "USER_ID")
   private User user;

   @ManyToOne
   @Cascade(value = { org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE })
   @PrimaryKeyJoinColumn (name = "CASEINFO_ID")
   private Case caseInfo;

   @ManyToOne
   @Cascade(value = { org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.SAVE_UPDATE })
   @PrimaryKeyJoinColumn (name = "ROLE_ID")
   private Role role;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.