Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the Java Persistence API (JPA).

Hibernate provides Annotations for the ease of web development. Without using hibernate-mapping file, you can map a java class to the corresponding table with the help of JPA Annotations. Some of the JPA Annotations are listed below:

i. @Entity– It is used for making a bean class an entity bean, so it must contain a no-arg constructor.

ii. @Table– It is used to create a table for the annotated entity.

iii.@Id– The primary key in the table is denoted as @Id.

iv.@Generatedvalue– It is used to auto-generate the value of the @Id field.

v.@Column- It is used to define the details of the column to which an attribute is mapped.

  1. Add Dependencies

When using hibernate annotations, you need to add dependencies in pom.xml. Add dependencies between <dependencies>…</dependecies> tag. Some dependencies are listed below:

You need to add Oracle dependency manually. If you are using Mysql, then you need to add MySQL jar or dependency.

2. Create a POJO/ Bean/ Persistence class

The encapsulation of many objects into a single object is done by POJO class or Bean class.

All the variables of bean class are set as private with public setter and getter methods.

Here we use Annotations in the bean class.

Bean class-Book.java

3. Create a Configuration File

The name for configuration file should be hibernate.cfg.xml. It contains information about the mapping file and relational database.

hibernate.cfg.xml

4. Create the Application Class

    It is a class which contains the main() method used to run the application.

    Let’s create AppTest.java.

Annonation Hibernate

Table-Book01

hibernate annonation

Pin It on Pinterest

Share This