Introduction of Hibernate

Dilusha Amarasekara
3 min readDec 22, 2020

--

Hibernate Overview

Hibernate is a framework for persisting or saving Java objects in a database. if you want to download the hibernate framework. You can visit this database www.hibernate.org. hibernate can easily create applications and store retrieve objects from the database.

· Hibernate can handle all of the low-level SQL codes.

· Hibernate can minimize the amount of JDBC code you have to develop.

· Hibernate provides the Object-to-Relational Mapping

So, I’ll explain about ORM using this example.

So, if you want to tell how to hibernate maps java class or object to the database. you have to map your java class to a given database. in this example, there is a class called Student. It has four fields id, firstName, lastName, and email. And middle has a hibernate framework. Then the left side has an actual database table. Also, it has four fields like a java class such as id, first_name, last_name, email. Also, it mentions the data types of the particular fields. So we have to say to hibernate framework this Java class Student maps to the given table. And have to set up a one-to-one mapping between the fields and actual columns in the database.

Now we want to set up this mapping via a configuration file. Using XML or can set it up using Java annotations.

So, now we look at how to the saving a Java object with Hibernate.

· First thing is we want to create a java object.

Student hiberStudent = new Student(“dilusha”, “chamod”, “dilusha@gmail.com”);

You can see I mentioned the firstname, lastname, and email.

· Now we have to save this object to the database. so here we used session, which is a special hibernate object.

int stuId= (Integer) session.save(hiberStudent);

In this code, the data is passed to the object. Hibernate will take the java object on those mapping that has been defined earlier. Hibernate will take that information and store it in the appropriate table, in the appropriate columns. These all the things are worked by the hibernate framework.

We mention the session.save() then Hibernate will return the actual ID that’s been assigned to that entry.

Now we look at how to retrieve from the database using the primary key. We use session.get statement.

Student student = session.get(Student.class, stuId); so, hibernate find the Student class and save the data to the particular Id called.

stuIdQuery query = session.createQuery(“from Student”); List<Student> students=query.list();

If you want all the Student objects. Then you can get a list of those Student objects. That query name is “from Student”. Then it all the queries past to the query variable then query.list() list all the Student objects. . So this work we want to small step to finish this work. Also, hibernate minimizes a lot of the low-level JDBC code you have to write in the past.

Thank You!

Dilusha Amarasekara

--

--

Dilusha Amarasekara
Dilusha Amarasekara

Written by Dilusha Amarasekara

Undergraduate of University of Moratuwa. Full-Stack Developer

No responses yet