Solved

unique=true in hibernate

Posted on 2011-03-24
9
518 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 167 total points
ID: 35212272
Frankly, the example about students and phones does not seem very starightforward to me either (maybe someone else will eplain it to us),
however the concept of unique=true is really simple.
In this way you communicate to hibernate that according to your design this field cannot have the same
value in two different rows - this would allow to apply unique constraint to the corrresponding column
which stores this field in the table. This in many cases can make many database operations
more effective and would allow databse to do the check on insert
which would catcth possible mistkes long before they can create real problems for your data.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35213120
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 .
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35213127
Sorry i am getting your other question please tell bit clear !!
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35213152
sorry i am leaking little bit in english...
0
 
LVL 7

Author Comment

by:gudii9
ID: 35234958
I was confused on how to define unique inside mapping file for many-many, many-one, one-one etc. please advise
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 333 total points
ID: 35239326
This parameter are you can use in u can define like that !! what ever you need !
<many-to-one
  name="propertyName"
column="column_name"
class="ClassName"
cascade="cascade_style"
fetch="join|select"
update="true|false"
insert="true|false"
property-ref="propertyNameFromAssociatedClass"
access="field|property|ClassName"
unique="true|false"
not-null="true|false"
optimistic-lock="true|false"
lazy="true|false"
entity-name="EntityName"
/>

0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35239343
(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  
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 333 total points
ID: 35239356
Thats key element is common for many-to-many,one-to -one,many-to-one
0
 
LVL 7

Author Comment

by:gudii9
ID: 35380115
>>>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.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to determine if a string is a valid SHA value 7 54
Java syntax, or is it Selenium 6 59
Running JavaFX on the Raspberry Pi 27 277
JDBC performance 8 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question