Hibernate: Composite PK vs Surrogate PK Opinions

As I understand it, whenever I use @Id and @GeneratedValue in a long field inside a JPA / Hibernate object, I actually use a surrogate key, and I think this is a very good way to determine the primary key, given my not very good experience composite primary keys, where:

  • there is more than one combination of business value combinations that becomes unique PK
  • composite pk values ​​get duplicates throughout the table.
  • cannot change business value inside composite PK

I know that hibernate can support both types of PK, but I was not surprised by my previous chat with experienced colleagues, where they said that complex PK is easier to handle when executing complex SQL queries and stored procedure processes.

They continued to say that when using surrogate keys they will complicate things when you join, and there are several conditions where it is impossible to use some things when using surrogate keys. Although I wish I could explain the details here, since I was not clear enough when I explained this. Perhaps next time I will tell you more.

I am currently trying to execute a project and want to try surrogate keys, since it is not duplicated between tables, and we can change the values ​​of the business column. And when the need for the uniqueness of the uniqueness of a business combination, I can use something like:

@Table(name="MY_TABLE", uniqueConstraints={ @UniqueConstraint(columnNames={"FIRST_NAME", "LAST_NAME"}) // name + lastName combination must be unique 

But he still doubts about the previous discussion of the complex key.

Could you share your experience in this matter? Thanks!

+5
java database hibernate database-design
source share
2 answers

The first rule about any application is changing requirements. Period. So, something that seems like a good candidate for a PC today may not be a PC at all tomorrow.

A value is a good candidate for a PC if it contains the following characteristics:

  • It is unchanging. He will never change.
  • uniqueness. Two entries will never use the same identifier.

However, it is practically impossible to have anything in the real world with these characteristics in an eternal way. I mean, even if today something is immutable and unique, this does not mean that it will always be so.

So, go with surrogate keys whenever possible. Use natural keys only for legacy databases. And run away from those friends who suggested that natural keys are better than surrogate keys (just kidding) :-)

And, of course, you can apply the uniqueness rule with a restriction in the database (as in the example), which makes it impossible to share two records with the same value if this is a business rule. When the business logic changes in the future, you will be glad to see that you used a surrogate key; -)

But don't trust the random dude in stackoverflow for this. Read these two Wikipedia articles:

http://en.wikipedia.org/wiki/Surrogate_key

http://en.wikipedia.org/wiki/Natural_key

+10
source share

Hibernate seems to encourage the use of surrogate keys. I prefer to use composite keys, but I do not want to deal with the framework, and composite keys can become cumbersome to work in Hibernate. Therefore, to get the best of both worlds, I use a surrogate key, but also tag my potential composite key columns with @NaturalId, so that Hibernate creates unique constraints for me. The @NaturalId function is false by default, so if you need to update the composite key field, use @NaturalId (mutability = true).

0
source share

All Articles