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:









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




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


It defaults to noaction.

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


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.


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


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


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:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on 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!