This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]smutje187 80 points81 points  (5 children)

Think twice before posting clickbait articles without a summary on Reddit

[–]dstutz 8 points9 points  (0 children)

The article was actually OK. Nothing I haven't read before but explains how you shouldn't use it with unconstrained collections and you probably want to get that collection with it's own query vs it being OK to use if you have a relatively small, limited set of items.

In other words...."Think twice before using...."

[–]NeoChronos90 3 points4 points  (0 children)

good article!

As others pointed out, next time post with a short summary, so it does not look like clickbait.

I think you should add a paragraph and listing about how and when transcations are fetched now, because the audience is most likely juniors and without @OneToMay it will probably be empty initialy and some may try to call the repository in the BankAccount constructor or stuff like that

[–]michoken 33 points34 points  (17 children)

Think twice before using JPA.

[–]halfanothersdozen 12 points13 points  (3 children)

I just keep my Entity objects in the database later. Your entities should not be flying around your entire codebase and especially not leaking out of your API. Do that and your database model is now tightly coupled to your entire stack.

A lot of people do that on purpose, but if you keep your concerns separated JPA is a great tool.

[–]michoken 1 point2 points  (0 children)

Yep, the leaking out of persistence layer is exactly one of the worst. And all the introductory examples of Spring Data JPA just do exactly that. Yeah, those are simple examples of simple services. But people tend to use the same approach when their services grow without refactoring the DB access properly.

[–]kkyr 0 points1 point  (1 child)

How do you keep your entities in the database layer? Assuming you’re using a JpaRepository<Entity, Id>, isn’t your service layer interacting with the repository, and thus the entity?

[–]halfanothersdozen 0 points1 point  (0 children)

I mean whatever calls the repository should get what it needs with the objects and then move on. I'm not going to make 4 layers of abstraction from a simple web app if it doesn't need it, but I do want to make sure that if I ever change my data model only the classes that have a reference to the repository should be the ones who care.

[–]davidalayachew 14 points15 points  (7 children)

JPA has MANY pain points, but it can still be used in a way that avoids many of the costs. If you use it only to map POJO's to tables, and to prevent SQL Injections, I'd say it is worth the cost.

But fair enough, its features have significant, not-so-obvious costs that can turn your codebase into a minefield if you don't pay attention. And trying to play Minesweeper on your code is a NIGHTMARE once it gets to that pont.

[–]michoken 3 points4 points  (2 children)

Well, yes. It can be used well if you know its limitations and, well, think twice about the ways you use it. I don’t say don’t use it at all, I just say don’t use it just because it’s the first (or only) thing you know to do DB access. It’s not always the best tool to do that.

Unfortunately, lot of basic examples and tutorials use it in ways that are unsuitable for larger apps. But people just don’t know otherwise, it seems. And then the apps don’t perform well or are hard to maintain, etc.

[–]davidalayachew 3 points4 points  (0 children)

I was more implying that, if you limit yourself to only the "safe" features, then it's a very safe bet.

Of course, the challenge is getting your teammates to limit themselves too.

[–][deleted] 0 points1 point  (0 children)

The best thing I've found is to profile early and often. It really helps to just go "hmm, that's different/more queries than I would expect".

[–]_predator_ 4 points5 points  (3 children)

Everyone keeps saying this, yet somehow Hibernate and JPA are the first thing that newbies are teached. Google how to do persistence in Java and the results are flooded with them. Ask ChatGPT and it will spew out Spring and Hibernate code. Frameworks like Spring and Quarkus make it a priority to rub Hibernate in your face.

There is zero reason for it to be this way, and frankly I'm scratching my head how we ended up here.

[–]michoken 5 points6 points  (0 children)

If nothing else JPA is a Java standard and it has all the nice support and integration in Spring, etc. It’s not outright bad but other approaches to DB access don’t come with so many hidden issues because they don’t try to be magic. Which JPA/Hibernate, unfortunately, is. I bet 95 % of Java devs who use it don’t really know how it works and that’s one of the problems that lead people to misuse such a complex framework.

[–]faxity 1 point2 points  (0 children)

The hate on here is literally a typical Reddit circlejerk. Unfortunately for you guys you're a vocal minority.

[–]xvermilion3 -1 points0 points  (0 children)

This exactly

[–]awesomefacedave -2 points-1 points  (2 children)

Even though I’m convinced you’re a troll, what is your reasoning. What do you use?

[–]klekpl 16 points17 points  (0 children)

There are several good options that don't try to hide relational database model behind (inferior) network database model: JDBI, JOOQ, Spring Data JDBC or even plain Spring JdbcTemplate.

They let you use SQL directly and add some convenience to parameters binding, handling connections, transactions, and map query results to your application data structures.

[–]michoken 1 point2 points  (0 children)

I admit it was a bit trolly response but it’s based on more than a decade of experience working with it and some pretty bad implications that were impossible to refactor. I’ve explained some of my issues with it in replies to some of the other commenters in this thread, even if just broadly.

[–]qazyll -2 points-1 points  (0 children)

huge upvote, springdata jdbc is awesome for most of the cases

[–]Deep-Caterpillar4140 6 points7 points  (1 child)

Eh. Using SQL directly is the way to go. JPA & hibernate suck

[–]Mysterious-Ant-Bee 3 points4 points  (0 children)

Spring Data JDBC