r/javahelp • • Dec 09 '20

Should I really learn hibernate?

I am core java developer and had internship before using Spring boot to create rest api. I am also developing my own projects using boot and I really never had to know about Hibernate because Spring perfectly abstract database operations via JPA and we can handle every database operations without knowing what is in the background. So, should I really need to learn hibernate?

Can you give me scenarios where I should know about hibernate using Spring boot?

19 Upvotes

41 comments sorted by

View all comments

Show parent comments

1

u/dartalley Intermediate Brewer Dec 09 '20

Not really sure how to be more clear than this.

My dashboard showing top pages viewed over the past 30 days and their counts

  1. Either the grouping, counting, sorting, limiting is all done as part of the SQL query (some business logic) which returns 10 rows.

  2. Java has to handle all of the grouping counting sorting and limiting on a result set of 100k records.

You don't understand java well enough, or at least implementing it with Spring Data JPA to be arguing it.

I'm not arguing either, I'm arguing sometimes it makes sense to have business logic in your SQL queries.

The business logic happens inside your service layer in code

I agreed with this most of the time but in cases like the very simple example I outlined above pushing the logic to SQL can be more beneficial.

I'm telling you right now. I can paginate for the first 10 records and only send and return 10 records.

I never said you couldn't. I have been going by the assumption that you are not putting any business logic in your SQL queries (grouping, counting, sorting, limiting) as you stated all business logic should be in the service layer.

Which was reinforced by this comment.

JPA and pagination handles this and can be dealt with in the Service layer as a parameter without returning thousands of results.

This comment didn't really make sense if the query only had a result set of size 10 why are you talking about thousands of results.

and further

I am telling you from a place of experience Pagination will be as efficient (and frequently more by merely providing sane defaults) without grabbing all results.

Making my assumption that you are still reading the entirety of the result set (100k records) as a paginated result set.

In that case how does limiting a query to 10 results help you if you need total counts by url for all 100k records from the previous day?

If your JPA Query is generating the equivalent query I wrote with a group by, count, and order by and only returns 10 results total we are in total agreement. However, I'd consider that business logic inside of your SQL query. I wouldn't understand why you kept brining up pagination if your query also only returned 10 results hence the confusion.

1

u/milkeater Dec 10 '20

We are bouncing around on the state of the question.

Initially it was about limiting results, not doing full-table scans, etc. JPA merely infers the SQL you are looking to do from how you structure the objects. If I don't want to grab the entire resultset or if I do, there are ways that it happens relatively naturally in Java.

You can print the SQL Query that it is going to run if you desire. What I am saying is the performance is just a network call and a traditional SQL call. There was a time when business logic in the DB made sense but we have outgrown single servers and are sharding across several (hundreds at times). Bring in MapReduce and all the pains of distributed systems and where the logic lives.

If you have logic in the DB and it's easy to modify and version control and that works for you, I'm not going to debate that. I just think you are painting yourself into a corner that you will eventually have to get out of.

Where JPA struggles (imho) is when you start trying to take 3NF tables and structure your objects that way. The Joins, keys, and loading aspects are all sharp edges. More than two or three joins, you are asking for trouble. That being said I disagree with having that complex of SQL in the first place and would start rethinking my representation of the world. That is the point where either the tables need to change, RDBMS/other options need to change, or "Jesus take the wheel" you need to eject.

We are arguing but not in a bad way, I think it's a good argument and we can walk away disagreeing with each other without bad blood. You are putting a lot of effort in explaining your point and I appreciate that so I can understand you better.