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"})
But he still doubts about the previous discussion of the complex key.
Could you share your experience in this matter? Thanks!
java database hibernate database-design
bertie
source share