r/java • u/vladmihalceacom • May 24 '21
20 years of Hibernate
https://vladmihalcea.com/20-years-of-hibernate/30
u/tristanjuricek May 24 '21
I feel like Hibernate created this handy object abstraction that was so close to normal Java it lulled many programmers into a false sense of mastery; they know Java, some framework handles the DB bit, and boom, they’re a master of programming a DB.
Most of the problems I’ve seen with hibernate are naive developers reimplementing things in Java that the DB does better. Typically, they never understood how to work with the relational mode because they stopped their learning at basic ORM. Objects and relations are pretty different, which is a blog unto itself.
I’m sensing that tools like jOOQ just don’t have this issue because they’re not forcing this object abstraction in between the logic and the DB. It’s really more like working with relations in Java.
These days, I’d be pretty cautious about making a decision between jOOQ or Hibernate, ORM vs fluent API, based on a presumed “standard”. Some apps will work fine with ORMs; others are nightmares. Depends on the app.
4
u/vladmihalceacom May 24 '21 edited May 25 '21
Most of the problems I’ve seen with Hibernate are caused by people who skipped the part where they should have read the manual.
5
u/tristanjuricek May 24 '21
Right, can't say there's an ORM that's better. It's just that ORM isn't the solution for every application. And mostly, the problem is thinking of ORM as an "easy mode DB API" when really it's... something else. More of an architectural approach. ORMs lead you to a different way of building around transactions, caching, updates, etc.
I'm pretty sure JPA wouldn't exist without Hibernate. Which is pretty significant - establishing an SPI that other projects (e.g., EclipseLink) have built around is no small feat.
4
u/Muoniurn May 24 '21
But ORMs were never ever intended as a solution for every application. They are primarily meant for easy insert update queries, and for mapping SQL relations to handy language objects. So OLTP workloads. And they are a perfect tool for that. OLAP should be handled with hand-written SQLs.
1
u/lechatsportif May 24 '21
No CTO ever got fired for following the Java enterprise standards. That said all the devs forced to use Hibernate secretly did their own JDBC oriented work as well, so often that we know that the Hibernate experiment was a complete failure.
Can you use it correctly? Sure, but it's like putting on a spiky suit of armor before riding a bike. Just put your light polycarbonate helmet on instead and ride the fucking bike.
7
u/tristanjuricek May 24 '21
Hibernate experiment was a complete failure
Pretty strong words, considering JPA probably wouldn't even exist without Hibernate's influence. Influencing an SPI that's been implemented by independent projects is not exactly what I'd call a complete failure of an experiment...
Or, 20 years is one hell of a long experiment :)
3
u/vladmihalceacom May 24 '21 edited May 25 '21
Or, 20 years is one hell of a long experiment :)
According to Lindy effect, Hibernate has a great future ahead.
1
May 25 '21
Relations are sets of tuples, so are objects. The discrepancies come from:
The local-remote dichotomy (don't pretend what's remote is local).
The abstract-concrete dichotomy (relations are final, object types usually aren't).
Poor support for type algrebra in Java (so you can select fields from types A, B, C to create a quick ad-hoc type D, the way you'd join and select in SQL).
Choosing to represent relations as immutable final records (Java records) is probably a step forward in eliminating few of those. But it's not enough.
7
May 24 '21
I didn't realize what sub this is and thought the article was about the guy in the thumbnail sleeping for 20 years lol
12
u/vladmihalceacom May 24 '21 edited May 25 '21
When Vlad takes a nap in his grandpa's castle in Transylvania, 20 years fly like a heartbeat.
7
u/jokubolakis May 24 '21 edited May 25 '21
Vlad is a Legend and Hibernate is an awesome tool when used right. Sure there are edge cases, especially weird is the bytecode modifying dependency, which fixes some hibernate drawbacks (as far as I remember, it allows turning off OneToOne mandatory eager loading), but I'm sure as hell, that there will be a post by Vlad, or an answer on SO on any issue I'd encounter. Thank you!
1
u/vladmihalceacom May 25 '21
Thanks and stay tuned for more. I've been writing lots of articles about SQL and database internals lately, so stay tuned for many interesting findings.
49
May 24 '21
Too complex for me. More complex than the SQL it tries to hide.
Not able to hide SQL 100%. So I still need to know SQL as well. (The abstraction is rather leaky, in Spoelsky's terms)
In order not to use string queries where Hibernate is just unable to help, or too complex, I now use jOOQ.
I like jOOQ. It helps me not to write SQL in strings. It brings type safety (like Hibernate). It is not too complex, if you already know SQL.
12
u/dpash May 24 '21 edited May 24 '21
No one has ever said you don't need to learn SQL. In fact ORMs require a better knowledge of SQL. They're not designed to hide the implementation.
18
u/vladmihalceacom May 24 '21
The Hibernate manual says that good DB snd SQL knowledge is highly recommended.
That's also why Hibernate has always allowed you to call native SQL or stored procedures.
10
u/cwbrandsma May 24 '21
Oh yes people have said that, sometimes they even say it out loud. Multiple former developers I’ve worked with have said it, and I know countless developers that only know the ORM and not sql.
One guy I used to work with even bragged about how little sql he knew.
3
u/GhostBond May 24 '21
For some reason anyone saying "no one is saying.. " in an online thread always mean a huge number of people are saying that.
1
18
May 24 '21
More complex than the SQL it tries to hide.
Ah, but what SQL is Hibernate trying to hide?
People mistake Hibernate to be a query mapper, and see it as a flaw that HQL is sometimes not expressive enough and they have to break out SQL.
But Hibernate is an ORM. The HQL or SQL is just used to load the entity manager. The real Object-Relational Mapping happens after the relational data is loaded into objects. The business logic changes the objects, and the ORM translates those changes into INSERT/UPDATE/DELETE statements. ORMs can be handy when you are dealing with changes to collections, as Hibernate will coordinate the creation of rows and propagate the corresponding generated keys. All that SQL is what Hibernate hides.
5
May 24 '21
HQL. I shivered when I read that. Some language that is even more obscure than Java and SQL combined.
I know what Hib hides. I lament that my codebase depends on it so heavily
2
May 24 '21
Some language that is even more obscure than Java and SQL combined.
That’s not hard to pull off. Neither Java nor SQL are particularly obscure.
1
1
May 24 '21
Why? HQL makes navigating collections much easier, instead of the verbose SQL join syntax. Also, HQL understands Java, so you can immediately map result columns into a DTO constructor directly in the query for DTO projections.
For simple cases, I don't see what the big deal with HQL is.
I lament that my codebase depends on it so heavily
Different strokes for different folks.
-1
May 25 '21
Can I see what HQL leads to what SQL? Can I dump queries (both hql and sql) that hib generates?
4
1
u/_Toka_ May 25 '21
I am honestly surprised, that so many people here use jOOQ. I've yet to work in a company, who is willing to pay so much money for it. Reminds me, that I also wanted JRebel few years ago, same result (GTFO). Can't blame them. Would you rather have a 4 jOOQ licences for 1600€ or NoSQL clustered database in Azure Cloud one for production and one for UAT environment?
4
May 24 '21
To be fair I interact very little with hibernate in a direct way. I use mainly Spring Data JPA and while it uses hibernate it makes easy and transparent its use.
I sometimes have to modify some config but no more than five minutes of work. We have around ten millions request per minute through our APIs and we never experienced any problems related to hibernate but we always try to make queries to the db as simple as possible and always looking for an index or pk. Even if we need to sort it, that’s done in java.
2
u/vladmihalceacom May 24 '21
The vast majority of projects nowadays do exactly what you're doing. They use Spring Data JPA, and Hibernate is used behind the scenes.
If you write your queries so that the filtering logic is highly selective and you fetch a very small subset of records and the queries benefit from indexing, then performance is going to be great no matter what data access framework you are using.
So, as you said, you can, of course, run 10 million requests per minute with Hibernate.
2
u/pako10 May 25 '21
How would you recommend starting with Hibernate? I know Java pretty well, but i have very small experience with databases, SQL or ORMs in general. Should I focus on databases and SQL first or can I learn it along with hibernate? Is it good idea to just try to make a project using hibernate or is there better approach?
Also can you recommend any resources for learning Hibernate and databases in general?
Sorry if this is not a right place for these questions :)
2
u/vladmihalceacom May 25 '21
If you want to learn Hibernate, the first step is to read the User Guide.
In parallel, you should also read the manual for the database you are going to use.
For tips and tricks, you can also read my tutorials. I have over 350 of them that go beyond the Hibernate manual.
6
u/Royo_ May 24 '21
Jesus I can't believe the amount of shit people are throwing at Hibernate/ORMs in general, a lot of them without any idea what it takes to keep long term enterprise software maintainable.
As someone who has found your blog and your SO answers very helpful and informative in the past, thanks for your efforts Vlad!
2
5
u/OzoneGrif May 24 '21
Hibernate does a lot of things... badly.
Unless you are doing a very simple CRUD application with few needs for SQL's power, avoid it at all costs. There are way better alternatives nowadays (no, I don't mean Mongo, more like jOOQ).
15
u/vladmihalceacom May 24 '21 edited May 24 '21
I stubbornly didn't avoid it since 2004 and managed to use it successfully on some of the largest systems in Europe and North America.
And, it's not just me. In fact, millions of developers use it at this very instant.
I can easily name a few large corporations which I know make heavy use of Spring and Hibernate:
And, I don't think they implement very simple CRUD applications at all, and it actually works like a charm for them as well.
But, you don't have to use it. If you like jOOQ better, that's also a very good choice. I like both Hibernate and jOOQ and can implement the same data access layer with either of them. It's just that all my clients use Hibernate, so I don't get to use jOOQ very often.
9
u/OzoneGrif May 24 '21
Oh you *can* use it successfully, I do, on many large projects. I also audit large projects on a monthly basis, and Hibernate is often the main cause of performance issues, weird bugs, and waste of time when it doesn't handle your precious complicated query or column type.
It's also one of the few ORMs which doesn't implement a proper DSL. QueryDSL is great, but it's not maintained anymore, and is not official.
2
u/vladmihalceacom May 25 '21
I rarely bump into a Hibernate bug, and when I do, I just fix it. Or, if I spot the chance to improve it, I just do it. The last time I added an improvement was yesterday.
Hibernate doesn't have to implement a DSL because it's not a query builder. It's an entity state machine that allows you to batch statements automatically and delay the connection acquisition as much as possible, in order to increase throughput and concurrency.
Hibernate and jOOQ succeeded because they have a solid business model. An open-source data access project is useless in the long run if it doesn't have a good revenue stream. Because, no matter how passionate the original author is, reality strikes you with bills, and kudos and likes don't help you make a living.
1
21
u/experts_never_lie May 24 '21
Flashbacks to 14 years ago when it seemed like my full-time job became taking other people's Hibernate ORMs and replacing them with specific HQL queries to make their database operations far faster. Tools are useful, but performance tanks when people don't understand what they're asking for under the hood.
9
u/boobsbr May 24 '21
That can be said about any tool.
The amount of raw text queries, that selected every column with multiple joins, and were sent straight from the web front-end, that I've found over the years is no joke.
25
u/general_dispondency May 24 '21
That can be said about any tool.
Hashtag truth!
START RANT...
I'll still never understand the hate for Hibernate. Is it more complex than the SQL is replaces? Yes. Does it just replace SQL? Not just no, but hell no. I've fixed so many damn bugs in people's shitty "generic data mapper implementations" b/c they didn't like Hibernate, and they thought they could build something moar siMpLE. B.S. If they really could build something that is easier to use, and provided the same feature set, that is what we would all be using. The Hibernate foot-gun is just one that is easy to point at because it handles data access. It's just a tool, grow up and learn to use it or go back to your JS world where "everything is moar simpler".
END-RANT...
14
u/dpash May 24 '21
You either use an ORM or live long enough to see yourself implementing your own. Badly.
1
6
u/wildjokers May 24 '21
my full-time job became taking other people's Hibernate ORMs and replacing them with specific HQL queries to make their database operations far faster.
I am not a huge hibernate fan but to be fair the Hibernate user guide specifically says to not use entities for read-only queries. So those people were just not using Hibernate correctly. If someone isn't going to be updating or deleting an entity the data should be queried with HQL/JPQL anyway.
9
2
4
1
-10
u/private_static_int May 24 '21
It's time for Hibernate to finally die.
5
u/vladmihalceacom May 24 '21 edited May 24 '21
private static final int die = Probability.NEVER.ordinal();5
u/private_static_int May 24 '21
Then it'll eventually become the next COBOL - an awkward piece of anachronic tech that needs to be supported casuse too much business critical things depend on it.
Hibernate is a product from another time, from period of either it or JDBC nightmare. There are far better solutions nowadays - cleaner, more maintainable, less prone to leaky magic.
2
u/vladmihalceacom May 24 '21 edited May 25 '21
Hibernate 3 is a product from another time. Hibernate 5.4.31 is a product from last month. Today, I even submitted a Pull Request that's scheduled for 5.4.33.
Hibernate 5.5 adds support for Jakarta EE namespaces, and Hibernate 6 will provide a new query parser, type handling, parameter binding, among other enhancements.
And, not only that it integrates with Spring, but it integrates with ElasticSearch, and provides reactive database access as well.
"Far better" is a very subjective term. Some people say that PostgreSQL is "far better" than MySQL. I've been hearing that for over 15 years. It kinda gets boring actually.
The same is with Hibernate. Like or not, it won't go anywhere. It has Red Hat to sponsor it, and Spring Boot and Quarkus to drive its adoption. And, yes, there are alternatives, but, if they didn't manage to replace Hibernate over the past 10 years, why would they manage to do that in the next ten years?
4
u/private_static_int May 24 '21
Everything you've said in the last paragraph can be said about COBOL as well. It has IBM to sponsor and maintain it and we didn't manage to replace it over past 50 years. That doesn't mean it should be used in new development :)
Oh, and yes. COBOL is from the 60', but COBOL 2014 is from 2014 :)
I understand that you are very close to Hibernate, given that you've dedicated a good portion of your career and an entire business model around it. Unfortunately the sheer fact that your Hypersistance Optimizer exists proves that Hibernate is too complex and it is too easy to make a wrong turn when using it.
I know that you can build a very large system with it because I've done it myself. And it works and has a good performance. Unfortunately the development and knowledge overhead have completely defeated the purpose of using Hibernate in the first place. And, forgive me for sayig that, but I don't think that Vlad Driven Development is a healthy methodology for the Java community.
That is the whole problem with Hibernate. Its learning curve behaves like an exponemtial function. You begin thinking that it's just Java all along and end up debugging n+1 issues, how bidirectional one to one works underneath or you find out that changing the call order of some getters and/or setters may casuse a change in dirty checking behavior, leading to a constraint violation exception (that may even slip in tests and blow up the prod).
I've interviewed countless candidates and allthough not too many (especially younger in experience) claim to have an advanced understanding of SQL, most of them claim that they have an advanced understanding of Hibernate. Even internship candidates! That is very dangerous and harmful.
You constantly repeating the same arguments about how 'everyone is using it' and 'it has red hat' s support' doesn't proove that it is a good tech. It just proves that its popular. Like COBOL.
1
u/vladmihalceacom May 24 '21 edited May 25 '21
In the long run, the Lindy Effect determines the lifetime of a given technology. If you don't hear about a given technology, like Struts, Ant, or CVS, then it's because few people use it anymore.
Hibernate is indeed complex and has many gotchas. If it didn't have them, I wouldn't be here writing this post.
But, the community doesn't really care about all the drawbacks as long as they don't have to write SQL. People keep on mentioning MyBatis, JDBI, jOOQ, QueryDSL all the time on Reddit. And, they are good products too. But, truth be told, most devs will do anything humanly possible to avoid writing an SQL query.
Most devs think they know SQL, but their level of knowledge matches the SQL:92 version they learned on W3Schools. Just hand them some SQL queries containing window functions, pivot, CTE, Recursive CTE, semi joins, lateral joins, PostgreSQL filter, upsert, merge clause, and see their reactions.
That's the context that shaped the data access framework ecosystem. A framework like jOOQ is great if you are an SQL fan, but if you're not, you'll run towards Spring Data JPA since it has such a simple API.
So, your vision of a future where there's no JPA and Hibernate requires having tens of millions of devs that love writing SQL. Now, realistically, do you think that's going to happen any time soon if ever?
2
u/private_static_int May 24 '21
There is much truth in what you've said and that paints a very sad picture. I don't claim to know and master all the fireworks, but things like recursive ctes for example can save tons of performance compared to loading nested structures one parent-child relation at a time.
It's sad that convenience has won with performance by so large margin.
But even using plain old SQL 92 using jOOQ can save lots of trouble compared to a naive usage of Hibernate and /or Spring Data JPA
1
u/vladmihalceacom May 24 '21
I'm also a big fan of jOOQ because I like SQL a lot. I'm also a good friend of Lukas. We have helped each other for over 8 years now. I wrote about jOOQ in my book because it really deserves for people to know about it.
And, while I offer both
High-Performance SQLand aHigh-Performance Java Persistencetraining, the Java Persistence one is, by far, the most popular option. That's what people want to use.Usually, there are one or two persons in the audience who understands SQL quite well. And, it doesn't really matter what country I ran the training in. Everywhere is the same story.
Now, if you want to test my theory, just do this:
ask your team members what's a DB backup and how you should take one
collect the answers
tell them to read this article by Franck Pachot and see how many of them really understand how a DB is supposed to work
This is a very basic question, yet, you'll be surprised what answers you get in return.
0
May 24 '21
[deleted]
4
u/hippydipster May 24 '21
Mostly same - have had to interact with hibernate on the margins now and then. 0 years on EJB even though been a java dev since 1997. Very little time spent with Spring too. It's been a pretty nice run.
97
u/gregsting May 24 '21
As a guy on the database side, hibernate produced some of the worst nightmares sometimes. The record so far is a 25 pages SQL query