Exploring Java Hibernate
According to our 2020 Java Developer Productivity Report*, 51% of developers are working with Java persistence technologies like Hibernate, EclipseLink, or OpenJPA. But why are these persistence technologies so popular, and what makes Java Hibernate such a well-used example?
In this article, we’ll discuss Java Hibernate, what it is, why it’s widely used, and why it has a reputation for causing performance issues.
What Is Java Hibernate?
Hibernate ORM, or Hibernate, is a free, open-source, object-relational mapping tool (or ORM tool) used in Java development. It provides a framework to map an object-oriented model to a relational database.
Hibernate allows developers to construct Java objects, then define the relationships between those objects. Then it converts those objects into database schemas, and ultimately SQL queries. The resulting SQL queries are native to the underlying database.Back to top
Why Is Java Hibernate So Popular?
For us, there are four main reasons why Java Hibernate ORM is so widely adopted:
- Interfacing Incompatible Databases - On paper, object-relational mapping is a really good idea. It helps with intrinsic problems in interfacing otherwise incompatible databases. Java Hibernate, as an ORM tool, helps to bridge that gap between incompatible databases.
- Automatic SQL Query Generation - There is a common belief that developers don’t need to understand how to write SQL queries. Because Hibernate automates the SQL query writing process, it helps to separate the roles of a developer and database administrator.
- Helps Apps Remain Persistence Engine Agnostic - Pragmatically, applications should be persistence engine agnostic. It shouldn’t matter which database is being used — and Hibernate provides a way to accommodate that.
- SQL Injection Resistant - Hibernate provides a safer alternative to SQL queries — with ORM limiting the risk of SQL injection.
Troubleshooting Java Hibernate Performance Issues
Hibernate is often problematic and carries a propensity for generating inefficient queries and non-standard data relationships. These two issues can cause a variety of other performance issues, as we’ll detail below.
Non-Standard Data Relationships
Hibernate, in order to associate two sets of data, creates a foreign key relation table. The resulting requests to those data sets often become needlessly complex.
In order to increase performance for these non-standard data relationships and their queries, a developer may have to code workarounds with a particular database in mind — which can defeat the whole point of using Hibernate in the first place.
Perhaps most importantly, by relying on a developer to simplify or alter these actions developers are then asked to handle questions better left to a database administrator or data scientist.
Hibernate can be unwieldy when it comes to queries. It can create inefficient and excessive queries in a variety of situations — often by default.
As an example, a developer might write a query in college database that allows users to see if a student is enrolled for specific course. An ideal query would look just at courses relevant to that student’s track, but Hibernate might choose to query the full class table data instead of only the applicable data. This query gives significantly longer request times for large data sets when compared to a query that only accesses the requisite data.Back to top
6 Common Issues With Hibernate
The most common Java Hibernate performance issues are related to excessive or inefficient database queries. There are a wide range of reasons as to why Hibernate triggers these excessive queries, but they often relate to the common Hibernate performance issues below.
1. SQL Statement Logging
Every time your database gets accessed, Hibernate logs that action by default. But, in a well-utilized application, the sheer amount of times a given database is accessed (often unnecessarily) can cause issues for logging engines. At scale, these concurrent requests to multiple databases can make your logging engine a performance bottleneck. 2.
2. N+1 Select Issues
When using the default FetchType.LAZY, Hibernate is prone to excessive, transparent N+1 queries. Because the queries don’t hold session, they execute against the database each time the data is requested. For queries requesting large data sets, that can mean long response times. It also makes Hibernate ill-suited to streaming data sets. 3.
3. Individual Entity Updates
When we write object-oriented code, we are used to treating individual objects atomically, updating them and operating on them one-by-one. It’s easy then to get into this habit with Hibernate, but, developers must remember that every update made to Entity objects in Hibernate result in *actual database queries* being executed. Be sure to think about pushing batches of changes as opposed to individual changes to the EntityManager.
4. Eagerly-Loaded Associations
Hibernate fetches eagerly for one-to-one relationships, often causing excessive queries. If you have more than one table trying to join with another table, instead of fetching the applicable data entry, Hibernate will fetch the entire table. That, by default, is inefficient. And, depending on the size of the table, scale of the application, and number of necessary joins, that inefficient query can lead to performance issues. 4.
5. Hibernate Can Generate Inefficient Queries
Many developers expect Hibernate to generate efficient queries. Unfortunately, many of the automatically generated SQL queries are inefficient. The takeaway? Hibernate can’t generate queries that outperform a human coded query. For some teams, the ease of generation might be worth it.
But, for larger teams or larger applications, those inefficient queries (and the dev time needed to fix them) can pose a significant hurdle to application performance and development. Sophisticated and complex queries are almost always better left to human developers, Hibernate is only consistently good at simple CRUD operations against a relatively small number of records at a time.
6. Hibernate Has Sharp Edges
With most Java technologies, developers are encouraged to discover the ins and outs via hands on experience — but hibernate can have sharp edges for new developers. Running Hibernate in production without an ironclad understanding of the technology can have disastrous consequences (think deleting a database).Back to top
Final Thoughts on Java Hibernate
Complicated data structures need something more intuitive than a silver bullet like Hibernate to successfully build efficient, scalable queries. The automated queries provided by Hibernate are not as efficient as human-created queries — and the technology certainly doesn’t replace database administrators or data scientists.
Want to learn more about Java Hibernate?
TutorialsPoint has a nice collection of Hibernate resources that can help you get started.
Save Time on Development
Be sure to check out JRebel to save time on development. Try free for 10 days to see how easy it can be for your team to skip redeploys.Back to top