java - @UniqueConstraint and @Column(unique = true) in hibernate annotation -


what difference between @uniqueconstraint , @column(unique = true)?

for example:

@table(    name = "product_serial_group_mask",     uniqueconstraints = {@uniqueconstraint(columnnames = {"mask", "group"})} ) 

and

@column(unique = true) @manytoone(optional = false, fetch = fetchtype.eager) private productserialmask mask;  @column(unique = true) @manytoone(optional = false, fetch = fetchtype.eager) private group group; 

as said before, @column(unique = true) shortcut uniqueconstraint when single field.

from example gave, there huge difference between both.

@column(unique = true) @manytoone(optional = false, fetch = fetchtype.eager) private productserialmask mask;  @column(unique = true) @manytoone(optional = false, fetch = fetchtype.eager) private group group; 

this code implies both mask , group have unique, separately. means if, example, have record mask.id = 1 , tries insert record mask.id = 1, you'll error, because column should have unique values. same aplies group.

on other hand,

@table(    name = "product_serial_group_mask",     uniqueconstraints = {@uniqueconstraint(columnnames = {"mask", "group"})} ) 

implies values of mask + group combined should unique. means can have, example, record mask.id = 1 , group.id = 1, , if try insert record mask.id = 1 , group.id = 2, it'll inserted successfully, whereas in first case wouldn't.

if you'd have both mask , group unique separately , @ class level, you'd have write code following:

@table(         name = "product_serial_group_mask",         uniqueconstraints = {                 @uniqueconstraint(columnnames = "mask"),                 @uniqueconstraint(columnnames = "group")         } ) 

this has same effect first code block.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -