Skip to content
Advertisement

What is a natural identifier in Hibernate?

While reading through the Hibernate documentation, I keep seeing references to the concept of a natural identifier.

Does this just mean the id an entity has due to the nature of the data it holds?

E.g. A user’s name + password + age + something are used as a compound identitifier?

Advertisement

Answer

In Hibernate, natural keys are often used for lookups. You will have an auto-generated surrogate id in most cases. But this id is rather useless for lookups, as you’ll always query by fields like name, social security number or anything else from the real world.

When using Hibernate’s caching features, this difference is very important: If the cache is indexed by your primary key (surrogate id), there won’t be any performance gain on lookups. That’s why you can define a set of fields that you are going to query the database with – the natural id. Hibernate can then index the data by your natural key and improve the lookup performance.

See this excellent blog post for a more detailed explanation or this RedHat page for an example Hibernate mapping file.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement