Hibernate Annotations Example in Eclipse

The Hibernate Annotations Example in Eclipse tutorial shows you how to create hibernate 4 application with standard JPA annotations. This application will performs CRUD operations on database.

The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications. And Hibernate implements this JPA specification. Hibernate also supports it’s own non-standard annotations to work with. I recommend you to work with Standards and use standard annotations.

Other interesting posts you may like

Create database Table

The stuff that we need to do before creating the Hibernate Annotations Example in Eclipse is MySQL installation on local computer and create database and table. You can aslo use MySQL Workbench or PhpMyAdmin to work with Mysql.

In this example, we create table by executing the script:

Project directory structure

Our Hibernate Annotations Example will have project directory structure like this:

Hibernate Annotations Example in Eclipse

Maven dependency

We will use hibernate and mysql dependency in project and here is our pom.xml:

Create Model
We create a simple POJO class named Student which be mapped to STUDENT table in database. This table is created in the above. This POJO class uses standard JPA annotations.

Let’s dig deeper:

@Entity indicates that this class is Entity Bean.
@Table indicates that database table will be mapped with this class. You can specify catalog and schema attributes with this annotation. @Table annotation is optional and unqualified class name is used as table name in this case.

@Id indicates that the field or group of fields is primary key of entity.

@GeneratedValue may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. GenerationType depends on the underlying database. For example, on MySQL, IDENTITY is supported (strategy = GenerationType.IDENTITY) and on Oracle SEQUENCE is supported (strategy = GenerationType.SEQUENCE).

@Column idicates that the class field will map with the column name in database table. This annotation is optional too and field name will be used as column name in this case. It also supports other attributes like unique, nullable, name & length.

Finally, don’t forget to It is a good practice and is required to work with collections or detached instances.

Create Hibernate configuration file

We need to provide for hiberate all stuffs like database dialect, driver class, url and account information to connect database. These stuffs is declared in file hibernate.cfg.xml like this

This file is placed in src/main/resources folder.

Create Hibernate Utility class

For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:

Create a program to perform CRUD operations on Database

Hibernate works with database by mechanism or steps like this

1. Open session
2. Begin transaction
3. Do something with entities (save/update/delete)
4. Commit transaction

We will also demonstrate these steps in our program. The program will perform CRUD operations like saving few records, list all of the persisted records from database, update a record in database and delete a record in database.

Now we create Hibernate Annotations Example in Eclipse like below.

Run the program and the output should be generated like this:

Hibernate Annotations Example in Eclipse

That’s all on the Hibernate Annotations Example in Eclipse tutorial.

Download complete source code, following the below link

HibernateExampleWithAnnotation.zip (298 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons