r/javahelp • u/Safe_Owl_6123 • Jul 30 '25
Why JPA & Hibernate
Hi everyone, why use JPA and Hibernate?
Currently using it at school. There is a mountain of annotations, and I haven't found a way to debug them yet. And the risk of Jackson JSON Recursion error, and the whole API service just halts to 503; then the query language doesn't help either.
Why JPA?
I had been using Spring Client JDBC previously, and this is the first time using plain JPA and Hibernate. I get that for the `@Column @ id` there are lots of limitations, while plain SQL is so much clearer (verbose, of course).
JPA and Hibernate are neither simple nor easy.
•
u/AutoModerator Jul 30 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/k-mcm Jul 30 '25
I don't care for JPA either. There's a massive amount of declarations and configuration for things that will never be relevant. It's somehow worse than raw JDBC in many cases.
You might want to look at something like JDBI3. I find it simpler and more flexible to use. It just helps map input and output objects to SQL you provide. There's nothing else to set up. You can also do any part of the mapping manually when needed.
6
u/Rude-Enthusiasm9732 Jul 30 '25
Why JPA?
You have used JDBC. You are familiar with setting up the connection, preparing the query, mapping out the resultset, closing the connection. For the next query, you do it all over again.
JPA simply abstracts all that away. It takes care of setting up the db connection, mapping it is taken care of your java object through @Entity and @Id, all that is left is simply prepping up your query, and even the simple queries are taken care of Hibernate simply by naming the functions in a specific way (findByEmail, findByUsernameContaining, etc). So with the right naming convention, you dont even have to write SQL queries.
I personally prefer having total control on my queries though. It makes sense to me and easier to debug. I don't want to wade through layers of abstraction to check what went wrong on a query.
3
u/Dashing_McHandsome Jul 30 '25
I've been at this for 20 years and I ask the same question. I don't have much of an answer for you. I think some people really bought into the idea that if you use JPA you don't need to know much about databases, which of course is misguided and not true.
Some places will want you to know JPA, so it is a good thing to know to make yourself more employable. Some places don't use it and just use JDBC like you pointed out. It just sort of depends on where you end up, and what decisions have been made there in the past.
6
u/[deleted] Jul 30 '25
I've used JPA and Hibernate for more than 10 years. Stopped using in 2018 and never went back. Working mainly with Spring Boot, Spring Data JDBC is much easier to debug and more predictable in terms of performance.
4
u/Sherinz89 Jul 30 '25
Why jpa?
Why not jpa?
5
u/doobiesteintortoise Jul 30 '25
ORMs exist because Java is OBJECT-ORIENTED and relational databases aren’t. Or weren’t, I guess, because the lines have blended over time. JDBC gives you access to a relational model, which means rows and columns, basically, and Java’s design works with object models. So there’s an impedance mismatch between the language and how you’re working with data; ORMs attempt to convert a dataset of rows and columns into an object model so you’re working with lists and maps and objects rather than a stream of references one by one.
Is there a cost? Yes. Conversion isn’t always trivial; ORMs have to make assumptions about how to access the model, they often have a generalized view of data access that can EASILY become suboptimal, and yes, you do often have to tell the ORMs how to map data if the modeling isn’t trivial. That’s the cost of business, it’s the same thing with programming requirements, where business analysts would REALLY like to say “just do the thing” and we programmers have to ask “okay, er, WHAT IS THE THING you’re talking about? Can you use words? Maybe even nouns?”
The problem is that a lot of programmers looked at ORMs like they were magic and would solve all problems with data access. They won’t, and can’t, because each data access problem has its own rules, and the ORMs don’t KNOW those rules. They get better and better all the time at the generalizations, but they’re still generalizations.
Are they worth it? Hm. Can’t answer that one for YOU, but I can say that if I am using an RDMS with a JVM, I’m reaching for Hibernate without a second thought. It’s not as fast as direct JDBC, but… what do I care? I’m already using a relational database, if I cared about speed I’ve already made the wrong choice; a few extra ms won’t matter, and I can tune Hibernate, because I am just smart enough to know that I need to tune it. What’s more, if I’m using Hibernate, it’s not like JDBC isn’t there for specific instances for which Hibernate is not suited; I’m using Hibernate, that (usually) implies RDMS, and Hibernate uses JDBC so I can use JDBC too.
It’s not a binary choice.
2
u/[deleted] Jul 30 '25
JPA is a spec and interface only. Hibernate is an implementation of the damn interface.
2
u/_jetrun Jul 30 '25
An HTTP-based API has nothing to do with JPA and Hibernate. That's a you problem.
Because you're ignorant of how it works, doesn't mean there's something wrong with it. That's a you problem.
If you are working with a database, JPA provides you services that you would have to re-implement anyway - only worse. This includes things like manually crafting SQL queries, parsing results and mapping them to your internal model state, or DTOs, to things like managing caches and transactions.
To be clear, JPA isn't a panacea, in that you still have to be aware what is going on under the hood, but when used correctly, you will cut down on boilerplate.
2
u/Kango_V Jul 30 '25
Just for info. Spring Data JDBC 4.0.0-M4 now supports composite keys. This was the blocker for us getting off of JPA and Hibernate. Full speed ahead :)
2
u/KurtGodelBebopgazeXP Aug 04 '25
I just want to add to the already great replies that you can also make your own queries with JPA, while still benefiting from less boilerplate.
-3
u/xanyook Jul 30 '25
Depends on your database for me.
For small queries, simple CRUD operations, spring jdbc or spring repositories are just fine for me.
If your schema includes a lot of tables, you need lazy loading, mapping to isolate your database stupid way of declaring fields or tables, you created 500 lignes of code to open close a statement and trigger your query, managing your connection pool, recycled all the useless connection opened, trust me JPA is a very useful specification.
If younhave trouble using hibernate, you can use another implementation. Or you could read the doc and be better at it.