CRUD with Java, Hibernate and MySQL — Part 3

In Part 3 of this tutorial, we perform CRUD operations using Hibernate and MySQL. For more information about the Student object and Object-Relational Mapping, please refer to Part 2 of this tutorial.

Creating objects

Creating new records in the database can be achieved in three steps:

SessionFactory sessionFactory = new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(Student.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
//create the Student object
Student student = new Student("Paul", "Walker","paul.walker@gmail.com");
//start a transaction
session.beginTransaction();
//Save the Student object to the database
session.save(student);
//commit the transaction
session.getTransaction().commit();

Reading objects

Reading existing record from the database can be achieved in three steps:

SessionFactory sessionFactory = new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(Student.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();//Read the student
System.out.println(“Getting the student based on id: “ + student.getId());
Student readStudent = session.get(Student.class, student.getId());//specify PRIMARY KEY of the studentSystem.out.println(“Retrieved student : “ + student);//commit the transaction
session.getTransaction().commit();

Updating objects

Hibernate uses its own query language called Hibernate Query Language (HQL) which is similar in appearance to SQL. More information can be found here.

SessionFactory sessionFactory = new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(Student.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();//update email for all students
int rowsUpdated = session.createQuery(“update Student set email=’hiberate@gmail.com’”).executeUpdate();
System.out.println(“Rows updated : “ + rowsUpdated);session.getTransaction().commit();

Deleting objects

Deleting rows from the database can be achieved in three steps:

SessionFactory sessionFactory = new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(Student.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
int studentId = 3000;//Deleting a single student
session.beginTransaction();
System.out.println(“Retrieving student with id : “ + studentId);Student readStudent = session.get(Student.class, studentId); //specify PRIMARY KEY of the studentsession.delete(readStudent);delete Student id=3001
System.out.println(“Deleting student where id=3001”);
session.createQuery(“delete from Student where id=3001”).executeUpdate();//commit the transaction
session.getTransaction().commit();

Full-Stack Senior Software Engineer | AWS Certified Developer Associate | AWS Certified Solution Architect Associate | Technologist | Blogger

Full-Stack Senior Software Engineer | AWS Certified Developer Associate | AWS Certified Solution Architect Associate | Technologist | Blogger