Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

unique=true in hibernate

I would like to know more about unique=true in hibernate. Where and how and why to use it. How to use it for many-many, one-many, many-one, one-one Any ideas, resources, links, sample code highly appreciated. thanks in advance.


Getting more confused especially with one-one where for example many students can have same address in this scenario STUDENT_ADDRESS column under student table is set to unique in STUDENT.hbm.xml file.
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html

similarly in one-many example where one student can have many phone numbers the phone id kept as unique within set collection of many-many tag.
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html
please advise.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sathish David  Kumar N
unique is the one of  the property element  used by Hibernate's DDL generation .
To create the unique index in database.. You will not do anything with that hibernate will take care while ur using unique =true ! then Hiberante will increase the index value .
Sorry i am getting your other question please tell bit clear !!
sorry i am leaking little bit in english...
Avatar of gudii9

ASKER

I was confused on how to define unique inside mapping file for many-many, many-one, one-one etc. please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(1)name: The name of the property.  
(2) column (optional): The name of the foreign key column. This may also be specified by nested <column> element(s).  
(3) class (optional - defaults to the property type determined by reflection): The name of the associated class.  
(4) cascade (optional): Specifies which operations should be cascaded from the parent object to the associated object.  
(5) join (optional - defaults to select): Chooses between outer-join fetching or sequential select fetching.  
(6) update, insert (optional - defaults to true) specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.  
(7) property-ref: (optional) The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.  
(8) access (optional - defaults to property): The strategy Hibernate should use for accessing the property value.  
(9) unique (optional): Enable the DDL generation of a unique constraint for the foreign-key column. Also, allow this to be the target of a property-ref. This makes the association multiplicity effectively one to one.  
(10) not-null (optional): Enable the DDL generation of a nullability constraint for the foreign key columns.  
(11) optimistic-lock (optional - defaults to true): Specifies that updates to this property do or do not require acquisition of the optimistic lock. In other words, dertermines if a version increment should occur when this property is dirty.  
(12) lazy (optional - defaults to false): Specifies that this property should be fetched  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>>unique (optional): Enable the DDL generation of a unique constraint for the foreign-key column. Also, allow this to be the target of a property-ref. This makes the association multiplicity effectively one to one.  


please elaborate on this. I was not clear.