Course Content
Java Data Manipulation with Hibernate
Java Data Manipulation with Hibernate
Hibernate. 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.
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.
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:
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.
Thanks for your feedback!