Hibernate 5 Tutorials

0

Hibernate 5 Tutorials

 Now Hibernate 3 and 4 are deprecated and Industry prefer Hibernate 5 and Hibernate 6. You  have knowledge of Hibernate 5 also if you have knowledge Hibernate 3 and four. Many Updating added in Hibernate 5


Now I am explaining Step by Step


1)  Open Eclipse Create Maven Project, Select Internal under Category and select QuickStart archetype

2)  Add Dependency under pom.xml file of hibernate and MySQL Connector

<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.scs</groupId>

  <artifactId>Hibernate5</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>Hibernate5</name>

  <url>http://maven.apache.org</url>

  

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

      <dependencies>

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-core</artifactId>

            <version>5.4.18.Final</version>

         </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>8.0.20</version>

        </dependency>

    </dependencies>

</project>

3)  Create hibernate.cfg.xml file 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  
    <property name="connection.url">jdbc:mysql://localhost:3306/ierp</property>
    <property name="connection.username">root</property>
    
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
  
    <!-- 'show_sql' set true to check sql statements on console else set to false -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <!-- use 'create' to Create tables on application startup else use 'update'-->
    <property name="hbm2ddl.auto">create</property>
  
    <!-- JDBC connection pool -->
    <property name="connection.pool_size">5</property>
    <property name="current_session_context_class">thread</property>
 
     <!-- Domain Model classes to be mapped -->
    <mapping class="com.scs.Hibernate5.Student" />
 
</session-factory>
</hibernate-configuration>

4)  Create HibernateUtil.java file:-

package com.scs.Hibernate5;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
  
    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("build SeesionFactory failed :" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
  
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
  
    public static void close() {
        // Close all cached and active connection pools
        getSessionFactory().close();
    }
  
}

5)  Create App.java file

package com.scs.Hibernate5;

import java.util.List;

import javax.persistence.TypedQuery;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Student student = new Student();
        student.setFirstName("SHIVA");
        student.setLastName("GAUTAM");
        student.setContactNo("7805063968");
        session.save(student);
        session.getTransaction().commit();
      //  Query<Student> q = session.createQuery("From Student", Student.class);
        TypedQuery<Student> q = session.createQuery("From Student", Student.class);
        List<Student> resultList = q.getResultList();
        System.out.println("total sudents:" + resultList.size());
        
        for (Student s : resultList) {
          System.out.println("student : " + s);
        }
      }
    }

Download Tutorials


We can create hibernate.properties file in place of hibernate.cfg.xml file also.


hibernate.connection.driver_class = com.mysql.jdbc.Driver

hibernate.connection.url = jdbc:mysql://localhost:3306/ierp

hibernate.connection.username = root

hibernate.c3p0.min_size=5

hibernate.c3p0.max_size=20

hibernate.c3p0.timeout=1800

hibernate.c3p0.max_statements=50

hibernate.dialect = org.hibernate.dialect.MySQL5Dialect




Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)