Want to add Unique Constraint for the combination of 2 diff diff columns
            Asked
            
        
        
            Active
            
        
            Viewed 680 times
        
    1
            
            
        - 
                    1https://docs.oracle.com/javaee/7/api/javax/persistence/UniqueConstraint.html – JB Nizet Aug 21 '18 at 06:22
- 
                    Please refer the link https://stackoverflow.com/questions/11955952/unique-constraint-over-multiple-columns – Meiyappan Kannappa Aug 21 '18 at 06:23
- 
                    I need to know to configure the combination of 2 different column combinations and 2 different column combinations, Not only the combinations of columns but different different combinations of columns I want to know – Raj Singh Aug 21 '18 at 06:26
- 
                    The attribute is named `uniqueConstraints`, with a final `s`, indicating that you can pass several `UniqueConstraint`s. – JB Nizet Aug 21 '18 at 06:30
- 
                    @JBNizet but in single combination, But I wanna do in two different combinations, like suppose you have a table with 6 columns(mbnumber,accno,nickname,id,name and remarks) and wanna to define unique on (mbnumber+accno) and (mbnumber+nickname) – Raj Singh Aug 21 '18 at 06:33
- 
                    Read my comment again. You can pass several UniqueConstraints: `uniqueConstraints={ @UniqueConstraint(...), @UniqueConstraint(...) }`. – JB Nizet Aug 21 '18 at 06:35
- 
                    @JBNizet yup , thanks bro It happen. I am doing it in wrong way now its works thanks – Raj Singh Aug 21 '18 at 06:39
1 Answers
0
            
            
        There is no Hibernate annotation that checks uniqueness before insert/update. But there is annotation which will generate such a constraint to the database if automatic database creation is used:
Try this one: When hibernate create a table in DB create unique constraint combination of these two columns.
@Table(
    name="Table_Name", 
    uniqueConstraints=
        @UniqueConstraint(columnNames={"Column1", "Column2"})
)
 
    
    
        Angad Bansode
        
- 825
- 6
- 15
