Create SessionFactory in Hibernate5 without hibernate.cfg.xml

By | June 3, 2018
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 HibernateUtil.java 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
Review the following project structure.

So let us proceed to write 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

Step 3 – Hibernate utility class which is responsible to provide SessionFactory

Create a class named as HibernateUtil.java as below:

Step 4 – 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

Output

That’s All about How to Create SessionFactory in Hibernate5 without hibernate.cfg.xml

Leave a Reply

Your email address will not be published. Required fields are marked *