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
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>HiberbateSessionFactoryCreationWithoutConfigFile</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 – 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 42 43 44 45 46 |
package com.infotech.util; import java.util.HashMap; import java.util.Map; 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; import org.hibernate.cfg.Environment; /** * @author kishan Kumar */ public class HibernateUtil { private static StandardServiceRegistry standardServiceRegistry; private static SessionFactory sessionFactory; static { // Creating StandardServiceRegistryBuilder StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); // Hibernate settings which is equivalent to hibernate.cfg.xml's properties Map<String, String> dbSettings = new HashMap<>(); dbSettings.put(Environment.URL, "jdbc:mysql://localhost:4406/test"); dbSettings.put(Environment.USER, "root"); dbSettings.put(Environment.PASS, "root"); dbSettings.put(Environment.DRIVER, "com.mysql.jdbc.Driver"); dbSettings.put(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect"); // Apply database settings registryBuilder.applySettings(dbSettings); // Creating registry standardServiceRegistry = registryBuilder.build(); // Creating MetadataSources MetadataSources sources = new MetadataSources(standardServiceRegistry); // Creating Metadata Metadata metadata = sources.getMetadataBuilder().build(); // Creating SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); } //Utility method to return SessionFactory public static SessionFactory getSessionFactory() { return sessionFactory; } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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(); } } } |
Output
That’s All about How to Create SessionFactory in Hibernate5 without hibernate.cfg.xml