Key use in HIbernate by R4R Team

We found in Hibernate mapping the <key> element which is featured a few times. <key> appears anywhere the parent mapping element defines a join to a new table that references the primary key of the origonal tbale. <key> is also defines the foreign key in hibernate in the join table.


We take a example to understand the conversation of above:


<key 

column="columnname"

on-delete="noaction|cascade"

property-ref="propertyName"

not-null="true|false"

update="true|false"

unique="true|false"

/>


We can understand all above code in the small given description which is given below in table:


Tag

Description

column(optional)

It shows the name of the foregin key column. This is also be specified by nested <column> elements

on-delete(optional)

It defaults to noaction.

It specifies wheather the foregin key constrant has database-level cascade delete enabled.

property-ref(optional)

It specifies that the foregin key refers to columns that are not the primary key of the origonal table. It is provided for legacy data.

not-null(optional)

It specifies that the foreign key should never be updated. This is imlied whenever the foreign key is also part of the primary key.

unique(optional)

It specifies that the foreign key should have a unique constraint. This is implied whenever the foreign key is also the priamy key.

update(optional)

It secifies that the foreign key should never updated. This implied whenever the foreign key is also part of the primay key.


With the help of this we can do for system where delete performance is important, we recomment that all keys should be defined on-delete="cascade". Hibernate uses a database level on CASCADE DELETE constraint, instead of many individual DELETE statement. So we need to Be aware that this feature bypasses Hibernate's usual optimistic looking strategy for versionaed data.


In hibernate the not-null and update attributes are useful when mapping a undirectional one to many association. If we map a unidirectional one to many association to a non-nullable foregin key, the we need must to declare the key column using <key not null="true">

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!