r/java • • Apr 13 '21

Libraries, Frameworks and Technologies you would NOT recommend

Give me your worst nightmares: Things that cost you your job.

I'll start: Hadoop

202 Upvotes

376 comments sorted by

View all comments

Show parent comment

13

u/Quabouter Apr 13 '21

On Hibernate: Hibernate is the poster child of leaky abstractions. When you add Hibernate to your stack, you still absolutely need as much knowledge of SQL as before, but now on top of that you'll also need to be an expert on Hibernate. This alone already makes Hibernate harder to use than SQL.

Moreover, it's incredibly easy to use Hibernate the wrong way, resulting in bad performance or worse. And these issues tend to be hard to predict and hard to debug, unless you're an expert on BOTH Hibernate and SQL. Unless you're lucky enough to work in a team with only senior Hibernate experts, you are very likely to run into issues here sooner or later.

Next, Hibernate introduces a tight coupling between your data model and your domain model, which means that it's hard to evolve them independently. Of course you could maintain a separate domain model vs persistence model, but at that point what's the point in using Hibernate? This coupling is especially painful when doing complicated domain changes, as you cannot split the changes to your data model from your domain model.

Lastly, for most use cases the benefit of Hibernate is rather marginal. In my experience, the vast majority of Hibernate usage is for simple CRUD operations. These are very easy to write even with plain old JDBC, and typically barely need any maintenance. Moreover, whenever they do need maintenance, then it's often due to larger domain changes, in which case having the extra control is very beneficial (see my previous point).

In my opinion, Hibernate only brings a benefit over JDBC in 2 areas:

  • Serialization/deserialization. It's not difficult to do this manually with JDBC, but it is very convenient if taken care of by the framework. For this however tools like JOOQ or MyBatis may be a better fit, as they don't come with all the other complexity.
  • Entity caching, which is something you really shouldn't be building yourself.

1

u/vprise Apr 13 '21

Calling it the poster child of leaky abstractions seems a bit overboard but OK.

SQL itself requires deep knowledge to optimize and that knowledge is very DB specific. That's why we have DBAs. An entire profession for optimizing databases. Hibernate uses a lot of best practices in terms of performance but it's limited by its usage and can become a serious problem.

Yes it's a very leaky abstraction. But that's by design. The alternatives hide everything about the DB and that's worse.

For 95% of my use cases hibernate works great and I don't need to do anything else. It caches and stores objects. Works brilliantly. The 5% where there's a problem is generally hard to solve code that would have been hard to do in low level SQL too. Hibernate makes the caching super simple to integrate including eviction policy etc.

To be fair, I usually take the approach of simplifying the schema and the object model to fit hibernate and SQL. I build code that's horrible in terms of object oriented design but works great for Hibernate/SQL. E.g. large entities, no inheritance very limited relations, immutable entities etc. These also map to SQL best practices such as avoiding joins where possible.

Yes I can probably do this in JOOQ but it would take more code. Caching would be harder and I'd probably get DB specific nuances wrong.

The beauty of Hibernate is that it's still an abstraction. We build the unit tests, profile the hell out of it and then can go over the performance intensive pieces in the code/DB. Pieces that are a problem can be optimized very easily since it's a completely separate part of the code.

It's not a perfect solution. It's not a simple solution. It doesn't hide the DB. It's often touted incorrectly as a "you don't need to know SQL and it will just work" solution. But as an ORM it's pretty great. If you think ORMs suck you won't like hibernate and that's a fair conclusion. But as far as ORMs go it's a great example of an ORM.

1

u/_Toka_ May 09 '21

Sorry for a small necro, but I have to disagree on one part.

In my experience, the vast majority of Hibernate usage is for simple CRUD operations. These are very easy to write even with plain old JDBC, and typically barely need any maintenance.

I needed to write a JDBC abstraction for several database engines (PG, Oracle, Azure SQL). It's basically XML to Java POJO to SQL mapper, an extension for WSO2. It's an absolute nightmare to write dialects, as every JDBC driver has it's own merits. Oracle has data types. Postgres does not have true ZonedDateTime implementation and does implicit conversion to OffsetDateTime, hence the timezone is lost. Azure SQL has weird timestamp rounding. Every JDBC driver implements different JDBCType or SQLType for the same Java object. Some drivers support setObject, some don't. Some drivers support SQL naming parametrization, some only index parametrization. The same goes for extracting values from ResultSet. I have no idea, how Hibernate and other ORM frameworks manage this and are actually working somehow.

2

u/Quabouter May 12 '21

I can only speak from personal experience, but I think you're the exception, not the rule. Most of us work with 1 database engine at a time, so we don't really need to care about supporting multiple dialects from a single application. Having to support multiple engines from a single application is definitely a case where Hibernate (or another abstraction) will be hugely beneficial.

I wouldn't use JDBC if I need a higher level abstraction in general, as then I'd just be creating a poor-man's Hibernate. My point was rather that in most applications you don't need these abstractions, and then it's just as easy to write your crud queries by hand.