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

Touchable shop Pos system using Java

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

2 weeks ago

Build Your First Responsive Login Form Using HTML and CSS FlexBox

Creating a responsive login form is a crucial skill for any web developer. In this…

2 weeks ago

Build Crud API with Laravel 12

In this tutorial will teach  Laravel 12 CRUD API  by step. Laravel  10 CRUD Application …

3 weeks ago

laravel 12 image upload tutorial

In this lesson we talk about laravel 12 image uploading and display the image step…

3 weeks ago

Laravel 12 CRUD Application

In this tutorial will teach Laravel 12 CRUD Application step by step. Laravel  12 CRUD…

4 weeks ago

Conditional Statements in Python

Conditional statements in Python allow us to control the flow of execution based on conditions.…

2 months ago