hibernate 6 crud operations

realNameHidden - May 20 '23 - - Dev Community

For More explanation watch video

For More information watch below video

step :
create maven project

Image description

Image description

Image description

Image description

step 2: edit pom.xml

add maven compiler plugin

add hibernate-core, lombok, mysql-connector-java maven dependencies

pom.xml



<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.test</groupId>
    <artifactId>Hibernate6CRUD</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.32</version>
        </dependency>

    </dependencies>
</project>



Enter fullscreen mode Exit fullscreen mode

step 3: Directory Structure

step 4 : Create entity
Student.java



package com.test.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "sttab")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer sid;

    private String sname;

    private String sadd;
}



Enter fullscreen mode Exit fullscreen mode

step5 : HibernateUtil.java

Image description



package com.test.util;

import java.util.Properties;

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

import com.test.entity.Student;

public class HibernateUtil {
    static SessionFactory factory = null;
    static {
        Configuration cfg = new Configuration();
        Properties props = new Properties();
        props.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
        props.put(Environment.USER, "root");
        props.put(Environment.PASS, "root");
        props.put(Environment.SHOW_SQL, true);
        props.put(Environment.FORMAT_SQL, true);
        props.put(Environment.HBM2DDL_AUTO, "update");

        cfg.setProperties(props);
        cfg.addAnnotatedClass(Student.class);
        factory = cfg.buildSessionFactory();

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

    public static Session getSession() {
        return factory.openSession();
    }
}



Enter fullscreen mode Exit fullscreen mode

step 6: Insert.java



package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.entity.Student;
import com.test.util.HibernateUtil;

public class Insert {
    public static void main(String[] args) {

        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ses = HibernateUtil.getSession();
        try(factory;ses){
            //begin the transaction
            ses.beginTransaction();

            //create student objs
            Student st1 = new Student("sam","mumbai");
            Student st2 = new Student("jhon","pune");
            Student st3 = new Student("brock","nagpur");

            //save
            ses.persist(st1);
            ses.persist(st2);
            ses.persist(st3);

            //commit transaction
            ses.getTransaction().commit();

        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}



Enter fullscreen mode Exit fullscreen mode

exceute Insert.java

you will find output in console as below

Image description

table will be created in mysql new schema as below

Image description

step 7 : Retrive.java

to retrive student by id



package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.entity.Student;
import com.test.util.HibernateUtil;

public class Retrive {
    public static void main(String[] args) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ses = HibernateUtil.getSession();
        try(factory;ses){
            Student st = ses.get(Student.class, 1);
            System.out.println("emp with id 1 info "+st);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}



Enter fullscreen mode Exit fullscreen mode

Image description

step 8 : RetriveAll.java



package com.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.entity.Student;
import com.test.util.HibernateUtil;

public class RetriveAll {
    public static void main(String[] args) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ses = HibernateUtil.getSession();
        try(factory;ses){
            //select * from sttab
            List<Student> list= ses.createQuery("from Student",Student.class).getResultList();
            list.forEach(System.out::println);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}



Enter fullscreen mode Exit fullscreen mode

console output

Image description

step 9 : Update.java

updating one record



package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.entity.Student;
import com.test.util.HibernateUtil;

public class Update {
    public static void main(String[] args) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ses = HibernateUtil.getSession();
        try(factory;ses){
            //begin treansaction
            ses.beginTransaction();

            Student st = ses.get(Student.class, 1);
            st.setSadd("Delhi");
            ses.getTransaction().commit();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}



Enter fullscreen mode Exit fullscreen mode

before update

Image description

after update

Image description

console output

Image description

Step 10 : Delete.java



package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.entity.Student;
import com.test.util.HibernateUtil;

public class Delete {
    public static void main(String[] args) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ses = HibernateUtil.getSession();
        try(factory;ses){
            //begin transaction
            ses.beginTransaction();

            //get the student
            Student st = ses.get(Student.class, 3);

            //delete student
            ses.remove(st);

            //commit transaction
            ses.getTransaction().commit();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}



Enter fullscreen mode Exit fullscreen mode

Image description

table before delete

Image description

after deletion

Image description

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .