Spring boot

Hibernate crud tutorial create delete update search

In tutorial we going to teach Hibernate crud tutorial create delete update search.if you study Hibernate  this is best place where you learn.i attached video tutorials below how to do step by step

first you must learn database configuration.here 

create the package entity inside the package create the class

Student

package entity;
import javax.persistence.*;

@Entity
@Table(name = "student_table")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long studid;

    @Column(name = "studentname")
    private String studName;

    @Column(name = "fee")
    private double fee;


    public Student(long studid, String studName, double fee) {
        this.studid = studid;
        this.studName = studName;
        this.fee = fee;
    }

    public Student() {
    }

    public long getStudid() {
        return studid;
    }

    public void setStudid(long studid) {
        this.studid = studid;
    }

    public String getStudName() {
        return studName;
    }

    public void setStudName(String studName) {
        this.studName = studName;
    }

    public double getFee() {
        return fee;
    }

    public void setFee(double fee) {
        this.fee = fee;
    }


    @Override
    public String toString() {
        return "Student{" +
                "studid=" + studid +
                ", studName='" + studName + '\'' +
                ", fee=" + fee +
                '}';
    }
}

Create the AppIntilizer.

AppIntilizer

import entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
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 org.hibernate.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class AppIntilizer {


    public static void main(String args[])
    {
           Student student = new Student(2,"Anne",2000);
           saveStudent(student);
           AllStudents();
           updateStudent("Raja", 6000, 2);
           deleteStudent(2);
           findStudent(1);

    }

    public static void saveStudent(Student student) {
        try (Session session = HibranateUtil.getSessionFactory().openSession()) {

            Transaction transaction = session.beginTransaction();
            session.save(student);
            transaction.commit();
        }

    }

    private static void AllStudents() {
        try (Session session = HibranateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("FROM Student");
            List<Student> students = query.list();
            System.out.println(students);

        }
    }


    public static void updateStudent(String name, double fee, long id) {
        try (Session session = HibranateUtil.getSessionFactory().openSession()) {

            Student selectStudent = session.find(Student.class, id);

            if (selectStudent != null) {
                selectStudent.setStudName(name);
                selectStudent.setFee(fee);
                Transaction transaction = session.beginTransaction();
                session.update(selectStudent);
                transaction.commit();
            }
            else

            {
                System.out.println("Can't Find Records");
            }
        }

    }

    public static void deleteStudent(long id) {
        try (Session session = HibranateUtil.getSessionFactory().openSession()) {

            Student selectStudent = session.find(Student.class, id);

            if (selectStudent != null) {

                Transaction transaction = session.beginTransaction();
                session.delete(selectStudent);
                transaction.commit();
            }
            else
            {
                System.out.println("Can't Find Records");
            }
        }
    }

    public static void findStudent(long id) {
        try (Session session = HibranateUtil.getSessionFactory().openSession()) 
{
            Student selectStudent = session.find(Student.class, id);
            if (selectStudent != null) {

                System.out.println(selectStudent.toString());

            }
            else

            {
                System.out.println("Can't Find Records");
            }

        }
    }
}

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/dbbcast?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>

      <property name="show_sql">true</property>
      <property name="format_sql">true</property>


  </session-factory>
</hibernate-configuration>

HibranateUtil

import entity.Student;
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).addAnnotatedClass(Student.class);
                Metadata metadata = metadataSources.getMetadataBuilder().build();
                sessionFactory = metadata.getSessionFactoryBuilder().build();
            }

        } catch (Exception e) {
            if (standardServiceRegistry != null)
            {
                StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
            }
        }

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

}

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

 

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 day ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

3 days ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

4 days ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

1 week ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

3 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

3 weeks ago