Spring boot

Hibernate Tutorial Introduction IntelliJ IDEA

Hibernate is an object relational mapping model develop the framework for java.in article we use to get the current date and time and MySQL version using Hibernate (ORM).

you have to download the following dependencies

mysql connector jar

hibernate-core 5.3.29 download jar

after download the dependencies you have add into the lib folder i attached the video lesson below.how to make step by
step.

Create the Class Appinitializer

Appinitializer

import org.hibernate.Session;
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;

import java.util.HashMap;
import java.util.Map;

public class Appinitializer{


    public static void main(String args[]) {

        printDateTime();
        MysqlVersion();

}

    public  static  void MysqlVersion() 
    {
        try(Session session = HibranateUtil.getSessionFactory().openSession())
        {
            Object obj = session.createNativeQuery("SELECT VERSION()").getSingleResult();
            System.out.println(obj);
        }
    }

    public  static  void printDateTime()
    {
        try(Session session = HibranateUtil.getSessionFactory().openSession())
        {
            Object obj = session.createNativeQuery("SELECT NOW()").getSingleResult();
            System.out.println(obj);
        }
    }
}

HibranateUtil.java

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;
import java.util.HashMap;
import java.util.Map;

public class HibranateUtil {


    private static StandardServiceRegistry standardServiceRegistry;
    private static SessionFactory sessionFactory;

    static {
        try {

            if (sessionFactory == null) 
            {
                standardServiceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
                MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
                Metadata metadata = metadataSources.getMetadataBuilder().build();
                sessionFactory = metadata.getSessionFactoryBuilder().build();
            }
            
        } catch (Exception e) {
            if (standardServiceRegistry != null)
            {
                StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
            }
        }

    }
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}

hibernate.cfg.xml

<?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="connection.url">jdbc:mysql://localhost:3306/dbsms?createDatabaseIfNotExist=true </property>
    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver </property>
     <property name="connection.username">root</property>
     <property name="connection.password">root123</property>
    <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  </session-factory>
</hibernate-configuration>

I have attached the video link below. which will do this tutorials step by step.

 

admin

Recent Posts

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

6 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago