In this post, we will learn how to create SessionFactory in Hibernate 5. Here we are going to use MySQL database but you feel free to modify hibernate.cfg.xml to use other database as well.
Technologies and tools used for this application are –
- Hibernate 5.2.11.Final
- Eclipse Mars.2 (4.5.0)
- Maven 3.5.0
- JavaSE 1.8
- MySQL 5.5.57
Project Structure
So let’s start writing a hibernate application, which will print the version of the MySQL database using a client program(ClientTest .java).
Step 1 – Create a maven project
Refer below link if you don’t know how to create Maven project in eclipse:
How to create Maven project
Step 2 – Add jar dependencies to pom.xml
Below is the pom.xml file and add required Hibernate and MySQL connector dependencies to it.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.infotech</groupId> <artifactId>HibernateSessionFactoryCreation</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- MySQL connector dependency --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.5</version> </dependency> <!-- Hibernate 5.2.11 Final dependency--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.11.Final</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> |
Step 3 – Create a Hibernate configuration file
Create an XML file named as hibernate.cfg.xml under src/main/resources folder and write the following code in it.
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:4406/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration> |
Step 4 – Hibernate utility class which is responsible to provide SessionFactory
Create a class named as HibernateUtil.java as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.infotech.util; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; /** * @author kishan Kumar */ public class HibernateUtil { private static StandardServiceRegistry standardServiceRegistry; private static SessionFactory sessionFactory; static{ if (sessionFactory == null) { try { // Create StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder() .configure() .build(); // Create MetadataSources MetadataSources metadataSources = new MetadataSources(standardServiceRegistry); // Create Metadata Metadata metadata = metadataSources.getMetadataBuilder().build(); // Create SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); } catch (Exception e) { e.printStackTrace(); if (standardServiceRegistry != null) { StandardServiceRegistryBuilder.destroy(standardServiceRegistry); } } } } //Utility method to return SessionFactory public static SessionFactory getSessionFactory() { return sessionFactory; } } |
Step 5 – Create a main class and run application
Here is the Client class which print MYSQL version. which makes use of HibernateUtil,java to get SessionFactory Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.infotech.client; import org.hibernate.HibernateException; import org.hibernate.Session; import com.infotech.util.HibernateUtil; public class ClientTest { public static void main(String[] args) { try(Session session = HibernateUtil.getSessionFactory().openSession()) { // Check MySQL database version String sql = "select version()"; String result = (String) session.createNativeQuery(sql).getSingleResult(); System.out.println("MySql Database Version is:::"); System.out.println(result); } catch (HibernateException e) { e.printStackTrace(); } } } |
You May Also Like:
Latest hibernate distribution Zip file download link
Hibernate 5 distribution binary details
Create SessionFactory in Hibernate5 without hibernate.cfg.xml
Save and persist an entity example in hibernate
Hibernate CRUD(Create,Read,Update and Delete) example
Dirty checking in hibernate example
Understanding hibernate Configuration File
Why to use hibernate dialect?
Hibernate hbm2ddl property
What are the benefits of using hibernate?
If you have any feedback or suggestion please feel free to drop in below comment box.