Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how , where and when to use natural-id in hbm file in hibernate?

I searched a lot in google but i didn't find any satisfied answer. So any one any please tell me a senario, where to use natural-id in hbm file in hibernate. Any help would be greatly appreciated.

Thanks in advance.....

like image 578
user671032 Avatar asked Jun 06 '11 12:06

user671032


1 Answers

You are strongly encouraged to use a surrogate primary key on your tables, i.e. a column called id which is not derived from the business data. However there is most likely a combination of fields which form a sensible business key for a table. These fields should be non-null and immutable. This is what would be marked as the natural key and would also be used in the equals and hashCode implementations for that class.

If Hibernate were to generate your database schema from your mapped domain classes this natural key would be used to generate a unique index for the table.

like image 177
Alex Barnes Avatar answered Nov 27 '22 14:11

Alex Barnes