Understanding ORM with Hibernate
In this article, we will take a look at what ORM is and how it helps us to make our apps bug free and less error prone due to typos.
Hibernate is a complete solution for the persistence module in a Java project. It handles the application interaction with the database, leaving the developer free to concentrate more on business logic and solving complex problems.
But before we can begin implementing Hibernate related solutions in our apps, we’ll have to understand what it offers us. ORM and persistence!
Persistence
To start, persistence means to save data while the app is working and users are interacting with it so that our app can identify the user and show them relevant data. It is one a fundamental concept in application development.
Using SQL in Java
When we work with an SQL relational database in our Java apps, the Java code issues SQL statements to the database via the Java Database Connectivity (JDBC) API. Whether the SQL was written by us and embedded in the Java code, or generated on the run by Java code, we use the JDBC API to bind arguments to prepare query parameters, execute the query, scroll through the query result table, retrieve values from the result set, and so on. These are low-level data access tasks; as application developers, we’re more interested in the business problem that requires this data access.
These data access tasks are so tedious that we have started to look for a cleaner and a smarter solution which will remove all that boilerplate code for handling SQL Exceptions and writing all those queries again and again.
To avoid this tedious task, persistence through Hibernate was introduced.
Layered Architecture
In a medium or large-sized application, it usually makes sense to organize classes by concern. Persistence is one concern; others include presentation, workflow, and business logic.
In a layered architecture, there are some rules to be followed:
- Layers communicate from top to bottom. A layer is dependent only on the layer directly below it.
- Each layer is unaware of any other layer except for the layer below it.
Let’s look at a diagram to understand how layered architecture works.
ORM: Object/Relational Mapping
In short, object/relational mapping is the automatic (and transparent) persistence of objects in a Java application to the tables in a relational database like MySQL, using metadata that describes the mapping between the objects and the database.
It works mainly by transforming data from one form to other. Like changing an object from JSON to a class representation and then saving it to the database. This indicates certain performance penalties.
An ORM solution basically consists of the following four pieces:
- An API for performing basic CRUD operations on objects of persistent or entity classes
- A language or API for specifying queries that refer to classes and properties of classes
- A technique for specifying mapping metadata like data type of a property in an entity
- A technique for the ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions
Is ORM problematic?
There are many questions which arise when we start to use ORM in our apps and we must consider if it is a really good choice for use in a particular app. Let’s face some of these questions here.
- What do persistent classes look like?
Entities we want to save in a database are normal POJO classes. This is probably the biggest advantage of ORMs. We define classes and they hold normal properties with normal data types like int, float etc.
- How is mapping metadata defined?
Because the object/relational transformation is governed entirely by metadata, the format and definition of this metadata is important. To ease the use of ORM, metadata is usually defined as Java annotations. We all know how easy they are to use!
- How do object identity and equality relate to database (primary key) identity?
This is easy. We define an ‘id’ key in our POJO class and some metadata and that field is automatically mapped to the primary key in our database schema. In reality, it is even easier than how it sounds.
- How do we efficiently retrieve data with associations?
Retrieving associated data is as easy as getting an object’s any field in its POJO class through a setter.
Apart from these questions, there are a few other issues as well. But we can try to treat them as advantages and will list them in next section.
Why ORM?
Let’s look at some of the benefits of ORM and Hibernate:
- Productivity
As discussed earlier, SQL and persistence related code is usually the dirtiest part of our application and tedious as well. Hibernate, used together with the appropriate tools, will significantly reduce development time.
- Maintainability
Fewer lines of code (LOC) make the system more understandable, because it emphasizes business logic rather than handling errors in our code. Though, there may be times when Hibernate code requires more maintenance but ORM helps us to balance between these two scenarios.
- Performance
Many times we will hear that hand-written SQL will be as optimised as that which is created by an automated ORM tool like Hibernate, but we must not forget the effort required to do so and more importantly, the maintenance it requires.
Hibernate allows many more optimizations to be used all the time.
- Vendor Independence
Though this advantage is sometimes underrated, it is still very important. Many a time in our apps we face a situation in which we need to change the underlying vendor, like moving from a MySQL database to an Oracle 11G database. With Hibernate in the picture, it’s easy because it’s automatic! This means that we do not have to do anything and Hibernate will handle it accordingly based on Dialect we set in our app.
In addition, database independence helps in development scenarios where developers use a lightweight local database but deploy for production on a different database.
Hibernate
Hibernate is a full object/relational mapping tool that provides all the previously listed ORM benefits. The API we’re working with in Hibernate is native and designed by Hibernate’s developers.
Hibernate Annotations should be considered in combination with Hibernate EntityManager. It’s unusual that you’d write your application code against JPA interfaces and with JPA queries, and not create most of your mappings with JPA annotations.
Conclusion
In this article, we’ve covered the concept of object persistence and the importance of ORM as an implementation technique, specifically towards Hibernate.
In later tutorials, we will cover core concepts of Hibernate and how Hibernate core is expanding much faster than EJBs.
Stop by discoversdk.com for Java SDKs
Recent Stories
Top DiscoverSDK Experts
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment