How to define Primary key with Hibernate Annotation?

Primary key in Hibernate

In relational databases, there is an important concept of primary keys. In this tutorial, we will learn the use of Primary keys with hibernate. In the previous tutorials, we have defined the primary key but we have not discussed it yet.

Hibernate Annotation

The annotation that hibernates provides for defining an attribute of an entity as a primary key is @Id. In the previous tutorial, we have defined the Id attribute as a primary key and generate its getters and setters to access it because this attribute is private. @Id creates a column in the table of the database which well be marked as the Primary key. Now this created column has unique values in it.

Natural key and surrogate key

We have two kinds of keys a natural key and surrogate key. Natural keys are those keys that exist in the real world like registration numbers etc and surrogate keys are those that do not exist in the real world and we make them by ourselves. We are using the natural key we have to provide its value because of its real world significance. By providing the value we have an option of controlling it while in the case of the surrogate key we assign this job to hibernate and hibernate then maintain a primary key for us and we don’t have to worry for its uniqueness. The syntax for surrogate key Annotation is simply using @GeneratedValue with @Id. In the case of @GeneratedValue, we don’t have to set id by our self in the main class.

dont have to pass userid value with generated values

Hibernate provides us this feature in its “javax.persistence.GeneratedValue” package.

Generated value annotaton and its package

Now If we try to save two users in the databases without passing the Id in the “HibernateMain” then due to @GeneratedValue Annotation hibernate will automatically generate the surrogate key.

Saving Two new users

The final code for HibernateMain and User Details will look like this and hibernate.cfg.xml will remains the same.

HibernateMain.java

UserDetails.java

Test Run

In Eclipse we can also run our java application with short keys, Alt+Shift+X , j

Console

This console tells us that hibernate first drops the table if exists because we have set “hbm2ddl.auto” to create. After that hibernate will create the new table and for inserting the records in this table it will auto generate the sequence for the id by finding the last id and increment it by one.

We can also define various strategies for the Generated Value hibernate provides us these Generation Types.

generation types for generatedvalues

  • Auto : It’s up to hibernate which strategy to use in order to generate Primary Keys
  • Identity: Hibernate us Identity Columns as Primary Keys. Not every database supports this feature.
  • Sequence: Hibernate has Sequence object that will be used to maintain unique Primary Keys
  • Table: Hibernate will create a separate Table to maintain a unique Primary Key.

Default as AUTO.

Summary:

In this tutorial, we have accomplished following things.

  • How to create Primary Keys in Hibernate.
  • How to use Generate Values Annotation.
  • What options do we have with Generate Value Annotation.

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About Author: Uzair Ahmed Khan is Technical Content Writer, Computer Software Blogger, and Software Engineer. He has a keen eye on Java Technologies other Related Developments.

 

Leave a Reply

Your email address will not be published. Required fields are marked *