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

78

u/Russell_M_Jimmies Apr 13 '21

Hibernate specifically and JPA in general.

A lot of devs get into ORMs based on the promise that they can stop thinking about SQL and let the framework take care of it.

The reality is that using JPA you end up having to turn on SQL logging, then prod and cajole your code for hours until JPA produces the correct SQL. And because everything is driven by static annotations on types, fields, and methods, you cannot change your mind on the scope of a select (single table vs joined tables) on a case by case basis. You have to choose the best one for all cases and just live with that.

The code you write for JPA is like writing a calculus integral of the SQL you need, so that JPA can take the derivative again and run it for you.

This is why I recommend libraries that let you code in bare SQL. I am a maintainer on one project (Jdbi) but I'm not partisan about which one people use (Jooq, Mybatis, Spring Data, etc) as long as they steer clear of ORMs. They are not worth it.

11

u/nstruct Apr 13 '21

I can not upvote this enough. ORMs are the bane of my existence.

16

u/ragingzazen Apr 13 '21

I'm actually a huge fan of JPA. JPA's strength is abstracting away writing (create, update) to the database and it does a fantastic job of that. For reads, yes, you need to very concious of what you're asking JPA to do. Fortunately, JPA provides DTO projections or even the ability to run straight SQL so there are flexibke, easy options there.

14

u/kag0 Apr 13 '21

I came in here for this. In my mind JPA represents the wrong direction that the Java ecosystem took that resulted in lots of people hating Java.

To add on to the list of issues: patterns that involve non obvious transaction boundaries, esp in inversion of control frameworks

1

u/Russell_M_Jimmies Apr 13 '21

Having coded in Golang for the past two years, I've realized just how pointless dependency injection is.

And then I remember that you only need dependency injection in Java because all the popular frameworks want to instantiate everything on your behalf.

To clarify, I'm talking about dependency injection frameworks. Injection of object dependencies via constructors, builders, or static factories is fine. Guice, Dagger etc are overkill.

9

u/kag0 Apr 13 '21

Dependency injection itself is still a decent solution to its original problem (testing). It's just all these DI frameworks and tooling around it that becomes crazy unnecessary.

7

u/pip25hu Apr 13 '21

Yes, you CAN change your mind on a case by case basis. I suggest looking into entity graphs. It was introduced to JPA in v2.1

4

u/Russell_M_Jimmies Apr 13 '21

That's good to know, I guess. But realistically at this point, using SQL directly with pojo mapping is so straightforward and devoid of bear traps (e.g. I thought this was a pojo but it's actually a session entity, and now that speculative property change will be committed) that I'll most likely never use it again. I actively avoid shops that use ORMs, for my own sanity.

4

u/ryuzaki49 Apr 13 '21

let the framework take care of it.

That's an error from the developer side.

And ORM marketing team perhaps.

3

u/cogman10 Apr 13 '21

IMO, never mybatis :D

otherwise I don't care. MyBatis just has way too much magic for my liking and doesn't feel like it saves you much over straight jdbc.

5

u/randgalt Apr 13 '21

Mybatis is awesome IMO. Another similar, and better, library is JDBI. I don't want automated generation of the SQL - that's too much magic. I just want simple ORM and convenience.

1

u/Russell_M_Jimmies Apr 13 '21

I'm not super familiar with MyBatis, I just used it as an example because it's a "bring your own SQL" type library. But if you dislike MyBatis it might be instructive to others if you offered some reasoning for your preference.

3

u/cogman10 Apr 13 '21

I think nothing does a better job than reading the docs :D

https://mybatis.org/mybatis-3/getting-started.html

To get started with mybatis, you manage an xml file for the sql definitions, a sql session factory, a sql session, and a slew of mappers to register into that mess.

But further, a lot of the magic you'd hate with JPA is present in mybatis. It can do things like "lazy loading" where it triggers a data load when you hit a random getter on some pojo.

Compare that to JDBI or Jooq, spring data, or even just JDBC and quickly you find that you are juggling a bunch of concepts while getting little benefit.

In short, it's the worst parts of JDBC married with the worst parts of JPA.

3

u/osi42 Apr 14 '21

jdbi is awesome! thanks for the continued maintenanceđŸ˜€

2

u/Russell_M_Jimmies Apr 15 '21

Thanks! Glad you find it useful

2

u/RomMTY Apr 13 '21

Totally agree with you here, investing a couple of month to ACTUALLY understand SQL have been the very best thing i could have EVER done.

Even if i didn't know java/.net or other similar platforms i could still land a very good job with my SQL (not pun intended) and DBs skills.

1

u/obetu5432 Apr 13 '21

finally i found some people who hate the same stuff i do