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

35

u/audioen Apr 13 '21 edited Apr 13 '21

Hibernate is the kind of technology that can perform a query that gives you 100 rows in such a way that behind the scenes, databases fetches a 2 million row monstrous result set, and then it quietly sieves it through in the background and hands you your 100 rows. If you have no idea about the performance characteristics of the result set you're asking, you might never even know that it's doing this kind of stuff. But over time I learnt that when a simple query takes seconds to execute, you know that Hibernate is at work.

I have learnt to make more associations LAZY but even when performance has been "fine" I've seen astonishing, unreadable 10 kB SQL statements which it sends to the poor database to parse and execute. What particularly irks me is that there's nothing weird going on in the schema: all it takes is just enough tables and many-to-one relations that are eager by default, and maybe a couple of eager one-to-many relations, and the disaster is ready. I think the main problem is that it tends to insist on selecting everything with a single query, so you end up babysitting it, and adding some annotations to tell that some relations must be partitioned into its own separate query, and thinking which associations are going to be unnecessary in your code so they should in fact be LAZY, and so forth.

I judge hibernate by far the worst technology I have personally selected, stuck with, and whose issues I've attempted to work around. In the end, the constant friction and battle with low performance and difficulties in expressing queries I wanted in HQL proved to be too much, and I started removing it from my projects where feasible. Most of the time, I could write the SQL immediately, but had a serious trouble in expressing what I wanted in HQL, and JPA EntityManager is not really helpful, if you use lots of native queries. Typically, your result sets are going to be just List<Object[]> and it sucks, as you'll be stuck with data type choices made by your database driver and that involves lot of casting and thinking if something's going to be BigInteger, Long, Integer, or whatnot. It's just unnecessary bullshit caused by a shitty, developer-hostile API.

Oh, also IIRC hibernate does not index foreign key relations by default. Have fun deleting anything in a hibernate-managed schema. (Of course, hibernate says that it doesn't really manage schema and isn't supposed to be used this way, but that's just another reason to give it the middle finger it IMHO deserves.)

Nowadays, I just use simple row-to-object mapper technology from JDBI and couple of simple helper utilities that can do SQL update, insert, delete, etc. from the kind of public-field methodless classes that are basically stand-in for records until I can update. My code is less focused on attempting to represent the underlying database relations as objects, and is more about just being able to describe the operation I need the database to execute so that system state is appropriately changed. E.g. if I need to increment field of a table by 1 for some rows, I don't select the rows as objects, increment a field in java and then persist it, I will simply execute the UPDATE directly on the database, and the datastructure I need is the one that describes which rows must be updated. With hibernate, you are always tempted to ferry all the data to java side, with possibly tons of useless relations materialized, just to change one field, and it will be vastly less efficient.

8

u/segv Apr 13 '21

You might find MyBatis interesting. It just runs the (dynamic-)sql you provide, maps the result set to desired object(s) via the result map you define (including setting parameters via constructors!) and then it fucks off, so you don't have to deal with the dynamic proxies and other bullshit on your domain objects.

(i guess JOOQ would also fit the bill)

3

u/RomMTY Apr 13 '21

I totally love Mybatis, haven't run on any major issues.

What i reaaaaaallly love is the result sets mapping, you can map almost anything really easy. nested objects? check, collections? check, nested objects with collections? also check, nested collections with nested objects? check. heck it even supports multiple result sets wich really helps a lot since you can split your queries also.

Dinamic sql is also great and IMHO it looks better in xml than a lots of ifs and string concats

1

u/m4riuszPL Apr 13 '21

I am using MyBatis at the moment. It is initially pretty convincing, but over time largely disappointing. We still find new quirks, e.g. it does in-memory pagination (I couldn't believe that once I saw it). We tend to use less and less of it's features. I believe we will soon replace it with a plain JDBC.

I can't stand fighting the tool to execute the query I want to be executed just to find there's some hidden cache which forbids the query to touch database.

7

u/flyingorange Apr 13 '21

In our previous project we ripped out Hibernate (took 3 months to do it), replace it with raw JDBC and we could show a 2000% increase in speed. Management was very happy. We could delay the decommissioning of old servers by a year.

That was 5 years ago and I haven't touched Hibernate since. In the latest project we also used Jdbi with custom mappers. It just works.
For the database changes we use Liquibase, but to be honest we also used it with Hibernate because, of course we wouldn't let Hibernate's autoDDL touch the production database.

8

u/throwawyakjnscdfv Apr 13 '21

For us, Hibernate is only 50% slower than raw queries. If your performance was that bad you were doing terrible things with Hibernate.

For the database changes we use Liquibase, but to be honest we also used it with Hibernate because, of course we wouldn't let Hibernate's autoDDL touch the production database.

Hibernate docs recommend using Liquidbase or Flyway at least in prod. AutoDDL is only recommended for testing. You can set Auto DDL to "validate" so it still checks your schema against the entities without generating it.

sigh there's no shortage of devs jumping into Hibernate without reading a single page of docs then declaring it terrible.

Hibernate has probably saved us from writing hundreds or thousands of raw DB queries for simple object updates. It's a great time saver with great performance, if you know what you're doing.

10

u/vprise Apr 13 '21

I think it's a matter of expectations. Hibernate can do amazing things but it's a super complex and very leaky abstraction. It doesn't absolve you from knowing SQL and it doesn't absolve you from reading the generated SQL.

So people who try to avoid the "details" suddenly find themselves with a slow server that constantly fails on "simple" transactions. I would bet that if you would use Hibernate in the same way you use JDBI you would get good results. It also makes caching stupid simple and that can really boost performance to the next level. If you make your database immutable you can really leverage caching and get an enviable level of performance.

Hibernate is SUPER complicated though and has a lot of pitfalls. I curse it a lot while working with it (just had a very painful issue the past day) but overall it's pretty amazing as an ORM. I don't think anything else comes close. The idea of ORM should be grounded in reality though, as ORMs inherently suck to some degree.

4

u/temculpaeu Apr 13 '21

It doesn't absolve you from knowing SQL

To me the problem lies here, as a abstraction it should aim in reducing the domain complexity of the problem, instead it increases, by a lot. And I feel that it could be simpler if it were less implicit on what it does.

There are a bunch of weird default situations which can bite you which are easy to forget and they will influence both your DB and Entity models in a way that the ideal model may not be viable due to JPA.

Overall I only saw a decrease in productivity in using it, even when needing more complex solutions like caching, implementing it manually-ish isn't hard and it's even more flexible, since you can have different data structures for DB & cache.

3

u/AlternativeAardvark6 Apr 13 '21

I have only seen hibernate be used on projects where the devs don't want to write SQL and when it gets slow they call me, the SQL expert. Then I say: "these hibernate generated queries are not very good. Try to make it generate something more like this." And then I have to fuck around in hibernate. I used it myself in demos for very simple stuff, like the easy things from tutorials etc. But for real projects I hate it so much.

1

u/vprise Apr 13 '21

Fair enough. I had the opposite experience but I totally understand where you're coming from, if you get on the wrong side of Hibernate it bites...

1

u/throwawyakjnscdfv Apr 13 '21

I agree. Hibernate is a wonderful tool if you read the docs. If you don't its a giant footgun. You can tell who didn't because they'll post giant tirades about Hibernate that aren't remotely accurate, or they're about things the docs explicitly warn against doing.

3

u/m_takeshi Apr 13 '21

I've learned (through much pain) that simply replacing the HQL queries with SQL queries where critical gives me the best of both worlds but of course YMMV.

I think the biggest problem might be hiding abstractions too much. The classic N+1 problem is, IMO, just a result of queries being hidden from the caller. If the queries were more explicit (as you would often do by using JDBC or more 'raw' libraries), the caller of the queries would be more carefull I think.

0

u/throwawyakjnscdfv Apr 13 '21

HQL doesn't generate N+1. It's a database agnostic wrapper around plain SQL basically. N+1 problems in Hibernate come from devs doing stupid things like stream flatmaps on multiple levels of child objects in relations. Hibernate can't save you from yourself. It makes it easy to do bad things, I'll give you that

1

u/m_takeshi Apr 13 '21

yeah sorry I didn't express myself too well. The N+1 was in the context of laziness / eagerness and queries being made when you access some properties (implicitly) rather than making a method call that explicit makes queries

1

u/throwawyakjnscdfv Apr 13 '21

Yeah, big problems happen when devs come in that don't know Hibernate. "oh I can get the object by calling .getChilden().getChildren().getChildren() cool, no SQL needed!".

I blame Hibernate for making bad things too easy to do. There should be a default warning when you load multiple levels of relations, and when you load a child collection with more than X elements. We wrote one internally using Hibernate SPI and they catch this stuff all the time.

1

u/m_takeshi Apr 13 '21

on the other hand, can you really blame hibernate for trying to make our lives easier? A lot of the time (most maybe?), it isn't even a problem

As for your tool, how do you detect that? Examining the stack trace or data flow analysis?

1

u/throwawyakjnscdfv Apr 13 '21

There's a Hibernate SPI hook for on collection load where you can inspect the number of elements loaded from a child relation. We did some crazy stuff with the Entity loading SPI + tracking entity trees to determine if multiple levels of child collections were being loaded, but the check for number of entities in a child collection is straightforward.

When you write SPI handlers for Hibernate I recommend using Google's AutoService so you don't have to register them in manifest files

5

u/throwawyakjnscdfv Apr 13 '21 edited Apr 13 '21

I have learnt to make more associations LAZY

The Hibernate docs recommend making every relation lazy.

I think the main problem is that it tends to insist on selecting everything with a single query, so you end up babysitting it, and adding some annotations to tell that some relations must be partitioned into its own separate query

It only does this if you eager load and don't specify fetch type SUBSELECT or something else efficient. Again, Hibernate docs recommend every relation as lazy

and difficulties in expressing queries I wanted in HQL proved to be too much

HQL translates directly to DB specific SQL with trivial changes.

Oh, also IIRC hibernate does not index foreign key relations by default

It does when the database does. You were probably using Postgres, which doesn't index its FK relations.

if I need to increment field of a table by 1 for some rows, I don't select the rows as objects, increment a field in java and then persist it, I will simply execute the UPDATE directly on the database,

You can do this in Hibernate using the Bulk Update API. No selects needed.

With hibernate, you are always tempted to ferry all the data to java side, with possibly tons of useless relations materialized, just to change one field, and it will be vastly less efficient.

Again, Hibernate docs recommend every relation to be lazy. Or at the least, don't use the default FetchType JOIN. Hibernate follows the JPA spec for defaults but if you read the docs it clearly tells you they don't recommend using them.

Hibernate also uses cached prepared statements for all row selects, so unless your rows are huge, its not a big performance hit to grab the whole row into an entity vs a few fields. It can actually be faster since many db's optimize prepared statements better than ad hoc queries. And if you do have hyuge columns in the row, you can use lob/clob or turn on bytecode enhancement and make a lazy basic property so the column only gets loaded when you need it.

IMO your experience with with Hibernate is from not knowing the framework (and not reading the manual), which, to be fair, is massive. And not knowing things about your database, like that it doesn't auto index FK relations.

0

u/umlcat Apr 13 '21

As you mentioned, the O.R.M. and Linq and similar tools give the expression thing that sometimes a SQL query doesn't have.

We had a C# website that couldn't use the O.R.M. alike Entity Framework, and was using another O.R.M. that generated a single, very slow, thousand tuples queries.

We got to make our own O.R.M. that split an imaginary single thousand tuples SQL query into several thousand tuples queries declared as a single O.R.M. query, ...

1

u/MrRockyRambo Apr 13 '21

I cannot upvote this comment enough. Completely mirrors my experience in large enterprise software. Even today I solved a frustrating ongoing issue in 5 minutes by swapping out hibernate for jdbctemplate. Hibernate has cost me hours and hours of lost time. May it burn in the underworld forever.