This series of tutorials is for Hibernate. The tutorial will cover from setting up Hibernate to fetching the details from the database using criteria. This series of tutorials will provide in-depth knowledge of Hibernate Framework with examples associated with them.

Click on the option to Create a simple project and click on Next.

Provide the Group id, Artifact id, Name, and Description then click on Finish.

Now provide the following dependencies in the pom.xml file.
Go to src/main/resources and create a hibernate.cfg.xml file.
Here ".cfg" depicts that the file is a configuration file for hibernate.
Inside this file provide the following data for hibernate configuration.
When the POJO is required to be mapped to the database using hibernate then the file name should be provided in the mapping class. As an example in the configuration file, Demo.java file is created in the package com.sumit.playjava at path src/main/java.
The class file should be annotated with @Entity to consider hibernate as the mapping file.
This is about the setting up of hibernate environment. In the next post tutorial will be about setting data in the database.
Introduction
Hibernate is an open source ORM (Object Relational Mapping) framework which provides database connectivity for JAVA. It maps Java POJO (Plain Old Java Object) or model classes to the database tables and performs most of the database persistence tasks.
Setting Up Environment
Prerequisites
One should have little knowledge of Hibernate, Eclipse, and Maven(how the dependencies are created using pom.xml).
Setup
In this setup, the project is created using Maven, using Eclipse as IDE.Go to New > Maven Project then click on Next.

Click on the option to Create a simple project and click on Next.

Provide the Group id, Artifact id, Name, and Description then click on Finish.

Now provide the following dependencies in the pom.xml file.
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.15</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.3.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>0.9.28</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.28</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.4</version> </dependency> </dependencies> |
Go to src/main/resources and create a hibernate.cfg.xml file.
Here ".cfg" depicts that the file is a configuration file for hibernate.
Inside this file provide the following data for hibernate configuration.
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name = "hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name = "hibernate.connection.url"> jdbc:mysql://localhost:3306/playjava </property> <property name = "hibernate.connection.username"> root </property> <property name = "hibernate.connection.password"> playjava </property> <property name="hibernate.show_sql"> true </property> <property name="hbm2ddl.auto"> update </property> <mapping class="com.sumit.playjava.Demo"/> </session-factory> </hibernate-configuration> |
When the POJO is required to be mapped to the database using hibernate then the file name should be provided in the mapping class. As an example in the configuration file, Demo.java file is created in the package com.sumit.playjava at path src/main/java.
The class file should be annotated with @Entity to consider hibernate as the mapping file.