Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Hibernate. ORM
course content

Course Content

Java Data Manipulation with Hibernate

Hibernate. ORMHibernate. ORM

For quite some time, developers didn't find it comfortable or practical to use plain JDBC. The issue is that with such an approach, it's not always easy to work with data. It's much more efficient to store data in object classes and transfer it from objects to the database. This way of working with data is much simpler for any programmer. Therefore, people gradually started transitioning to using ORM frameworks.

ORM (Object-Relational Mapping) is a programming technique for converting data between incompatible systems. It allows developers to work with database data as if they were regular objects in their programming language, making it easier to create and manage database applications.

In the context of Java programming, such a framework is Hibernate. It's commonly used in almost any web application. Hibernate also offers compatibility with Spring Boot and many other frameworks.

Hibernate is an ORM framework for Java that simplifies the interaction between an application and a relational database. It maps Java objects to database tables and converts Java data types to SQL data types, automating the data query and retrieval process, allowing developers to focus more on business logic rather than database operations.

How Hibernate Works

Let's see how Hibernate works and what it does through a simple example. Suppose we have an Employee class with some fields, for example:

Such a class would be called an entity because it represents the employees table, which we worked with earlier.

Here's an example of this table to refresh your memory:

id name position salary hire_date
1 John Doe Software Engineer 60000.00 2021-01-10
2 Jane Smith Project Manager 65000.00 2021-02-15
3 Jim Brown Designer 55000.00 2021-03-01
4 Jake Blues System Analyst 70000.00 2021-04-20

As you can see, the fields and column names match in the table, and their data types also match. We can conclude that there is a correlation between them.

If we want to insert data into the database, it will be convenient to simply save an object of the Employee class. Similarly, when retrieving data, we can directly fetch it into an Employee class object.

This is the essence of ORM; we manage class objects because it's much simpler and more convenient for any Java developer. In the context of OOP, this is excellent because we continue working with classes and objects to achieve results and build the business logic of the application.

To get started, let's set up MySQL, which we will be using in this course.

Creating a Database and Configuring MySQL

Here is a list of SQL operations you need to perform in the TestDatabase to create the same employees table as mine. We will be working with this table.

Step 1:

Create the TestDatabase:

Step 2:

Switch to using the required schema. In most database management systems, you need to explicitly specify that you will be working with the newly created database:

Step 3:

Table Creation:

Step 4:

Inserting Test Data:

After these operations, you should have the necessary database and table, which we will be working with for some time in this course.

1. What is the primary purpose of ORM in programming?
2. What does Hibernate do in the context of Java programming?
3. In ORM, what is an 'entity'?

question-icon

What is the primary purpose of ORM in programming?

Select the correct answer

question-icon

What does Hibernate do in the context of Java programming?

Select the correct answer

question-icon

In ORM, what is an 'entity'?

Select the correct answer

Everything was clear?

Section 2. Chapter 1
some-alt