How, where and when to use natural-id in hbm file in sleep mode?

I searched a lot on google, but I did not find a satisfactory answer. So any of them, please tell me senario where to use natural-id in the hbm file in sleep mode. Any help would be greatly appreciated.

Thanks in advance.

+4
source share
2 answers

Your question is a bit vague, but the reason for using Natural-Id in the hbm file would be to automatically force things to be unique. This property must be assigned to fields that make your data unique. When you put an attribute in these fields, hibernate will generate unique indexes for you.

If your question Why should one ever use natural keys? There are too many to list here, but a simple example ...

If you decide to expose your data through web services, channels, etc., it is not recommended to provide the end users with the keys to your database. This is bad for many reasons, one of which is what happens if they store it and you lock yourself again? You are stuck! It’s best to speak in terms that they understand, which would be the things that are already present in the data (naturally;))

If you are interested in reading about it yourself on AgileData.org , you will like Natural vs Surrogate

+5
source

You are strongly advised to use a surrogate primary key in your tables, i.e. a column named id that is not derived from business data. However, most likely, this is a combination of fields that form a reasonable business key for the table. These fields must not be null and immutable. This is what would be marked as a natural key, and will also be used in equals and hashCode implementations for this class.

If Hibernate was to generate your database schema from your associated domain classes, this natural key will be used to create a unique index for the table.

+3
source

All Articles