Hibernate Configuration and JPA (Java Persistence API) Annotations with Example

Hibernate Configuration and More JPA  (Java Persistence API) Annotations

In the Previous tutorial of Hibernate Series, we have seen the concept of saving an object in the database with Hibernate. We have configured Hibernate with our application and database. We have also created an object to store it in the database. Annotations provide hints for Hibernate. Hibernate configuring file provides many important details for Hibernate. On the basis of these details Hibernate decide what to do with the object and how to store it. In this tutorial, we will discuss the Hibernate configuration file in detail.

Download Saving Objects with Hibernate Example Project

Query tool in pgAdminIII

Open the Query tool in pgAdminIII and write a query like this. Query tool can be opened by pressing Ctrl+E or by navigating to Tool – – > query tool

By executing the above-mentioned query we can view the table that we have previously created.

pgAdminIII Query Tool

hbm2ddl.auto Explanation

To understand hbm2ddl Let’s perform an experiment.

Edit HibernateMain as shown below.

This Line of code in “HibernateMain.java”
 

objectUser.setUserId(1);

objectUser.setUserName(“UserName”);

Will be replaced with
 

objectUser.setUserId(2);

objectUser.setUserName(“SecondUserName”);

And run the HibernateMain as Java Application this will remove the UserName and insert SecondUserName in the database.

Execute the query in the query tool of pgAdminIII, this will display only one record with value SecondUserName

We have not yet discussed the hbm2ddl in hibernate configuration file when hbm2ddll is set to create this will drop all the tables on each run and recreates it, which can cause the data loss that we have stored in the previous run. We will use update instead of creating the hibernate configuration file. Edit hibernate.cfg.xml as follows

This Line of code in “hibernate.cfg.xml”
<!– Drop and re-create the database schema on startup –>

<property name=“hbm2ddl.auto”>create</property>

Will be replaced with
<!—update the database –>

<property name=“hbm2ddl.auto”>update</property>

 

The update will tell Hibernate to update the database only when any changes occur. Again try to run the HibernateMain as Java Application.Now execute the query in the query tool of pgAdminIII, this will display both the records with no data loss.

pgAdminIII query tool

There is another thing that we have done in hibernate.cfg.xml , we have set show_sql to true this means that we are telling hibernate to print the SQL commands that it is executing in the background to perform our task.

There are some other annotations and their properties that we will look here. @Entity has a property called name we can use it as follows

Override Table Name

@Entity (name=”YourTableNameHere”) comes from javax.persistence.Entity

If we only use @Entity then hibernate will create the table with the name of the class name but when we use name attribute of the @Entity and specify a name in the quotes, this will create a table with the name specified in the string.

Override Column name

@Column (name=”YourColumnNameHere”) comes from javax.persistence.Column

By default hibernate picks the field name as the column names.If we want to restrict hibernate to use our field names as column names of the table. We will use this annotation with the fields, here attribute name is telling hibernate the custom name for the column.

We can also use multiple annotations on the same field, for example, we have a private field of integer type with name id. We want to make this field primary key and we also want to use our custom name to be used for the column name. In this type of scenario, we will use multiple annotations on the same field.

Update first 15 lines of the User Details.java as shown below

Output of overridden Table and Column Name

In HibernateMain.java update the code to insert third value in the database as shown below

This Line of code in “HibernateMain.java”
 

objectUser.setUserId(2);

objectUser.setUserName(“SecondUserName “);

Will be replaced with
 

objectUser.setUserId(3);

objectUser.setUserName(“ThirdUserName”);

run the HibernateMain as Java Application this will insert another record in the databse

pgAdminIII Query Tool

Hibernate Annotations

Annotations are placed on the top of the field. It can be placed on top of the functions/methods.

@Table (name=”Table Name”)

Package of @Table in hibernate is javax.persistence.Table. There are various attributes of the Table annotation that can be used with it these are

  1. name
  2. uniqueConstraints
  3. appliesTo
  4. indexes

With this annotation, we can set name for our tables. The difference between the @Entity and @Table will make sense when we write HQL (Hibernate Query Language), we will see the difference when we write HQL.

Example:

Add more member variables in the previous example and generate getters and setters for these member variables

Initialize this property in the Main Class using setters.

Saving of the object part of our previous code remains same

UserDetails.java

HibernateMain.java

hibernate.cfg.xml

TestRun:

Run as java Application, in the console, you can see the following output

According to the output, Hibernate first drops the table and then creates it again this is because of the hbm2ddl.auto set to create, finally hibernate inserts the record in the table. Hibernate have inserted new columns with the name of fields and with respect to the data types of the fields.In this insert hibernate is mapping the java datatype with the SQL datatypes.

@Basic

Package of @Basic in hibernate is javax.persistence.Basic;

@Basic has two properties that we can make use of, these can be shown in the figure below

basic hibrnate annotations properties

fetch attribute is used to tell hibernate to fetch field as lazy or eager

FetchType is provided in javax.persistence to define how to fetch fields

optional attribute is used to tell hibernate that field is nullable or not.

@Transient

Package of @Transient in hibernate is javax.persistence.Transient;

It is used without properties. If we want to skip the field and want it not to be saved then we use @Transient with that Field. it tells hibernate not to persist that specific field in the database.

@Temporal

Package of @Temporal in hibernate is javax.persistence.Temporal;

Temporal is basically the data which could have the date, time and timestamp in it. @Temporal has following properties that we can make use of, these can be shown in the figure below

remporal hibrnate annotations properties

TemporalType is provided in javax.persistence.TemporalType to define how to use this field

@Lob

Package of @Lob in hibernate is javax.persistence.Lob;

It is used without properties. Lob means the large object. We use it to tell hibernate that we are saving Pages of description as a Clob or Blob. Hibernate determine it with our used field type. If we use field  type as an array of characters it means Clob and if we use byte stream it means Blob.

Summary:

In this tutorial, we have accomplished following things.

  • hbm2ddl.auto Explanation
  • JPA – Java Persistence API Annotations Examples
  • Various JPA Java Persistence API Annotations

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.