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

•

u/AutoModerator Dec 09 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • 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://imgur.com/a/fgoFFis) 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:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

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.

15

u/LiteratureStriking Dec 09 '20

Hibernate is an implementation of JPA, so if you are learning JPA, you are learning Hibernate. Spring Data JPA uses Hibernate by default. It is good to know how Hibernate works when problems happen and you have to debug. Also, if you are trying to squeeze out maximum performance, you might have to use some Hibernate specific tricks even if most of your application uses the JPA interface.

2

u/milkeater Dec 09 '20

Came here to say this.

One thing to add:

Hibernate is a productivity tool that removes boilerplate software from the responsibility of the developer. It is a productivity vs flexibility tradeoff.

For a measurement of when I typically tell my junior engineers to switch over (because it can often be a gut feel), start with Hibernate. When your tables start to normalize and develop complex joins and you find yourself dealing with lazy/eager loading of complex child objects you are in the realm of switching over to more manual control.

You will now need to deal with Entity Managers, connection pools, ensuring you close connections (and catch that Exception that can be thrown in another Exception!), try with resources, etc etc.

Unfortunately in an enterprise environment there is often this upfront desire to pre-plan everything. It becomes very unnatural and prescription based. For the value and benefit of consistency (which is arguably not as consistent as one would think) using "Enterprise Frameworks", they often are wrappers around public frameworks with opinionated usages and security modifications.

In the end, something the entire team is comfortable is what I've found to be the best decision. If that means pushing back on the complexity of your object structures that could be an option. If it's leaning out the frameworks leveraged, sure. Anything that you write which doesn't provide value (boilerplate, creational, structural, etc) is lost time and you will not be paid more for being slow....they will just look to automate more of you.

1

u/[deleted] Dec 09 '20

So, frameworks are distinguished by IoC, where the framework calls your code instead of your code calling a framework. Spring is a framework, Hibernate is a framework; who is calling who? Where is the IoC? Which one is actually in control?

2

u/LiteratureStriking Dec 09 '20 edited Dec 09 '20

Depends, and it is a little bit more complicated than that.

Assuming you're running Spring Boot with the embedded Tomcat Server.

Tomcat runs the Servlet container, Spring MVC implements a Servlet. In this case, Tomcat is the IoC container. It is listening for HTTP requests and calls Spring MVC.

Spring runs the application container, yet another IoC container. Spring creates the EntityManagerFactory and injects the EntityManager into your Spring components. Spring MVC will call your Spring component.

Hibernate runs an EntityManagerFactory (JPA)/SessionFactory (native). Spring calls Hibernate to get EntityManger instances to inject, and you call EntityManager create/update/delete entities. Spring will call EntityManager to flush the session, and Hibernate will send SQL to the database. But, Hibernate will manage entities in the session, and can trigger a bunch of lifecycle hooks on the managed entities. So, the EntityManager kind of IoC, I guess.

So, it's multiple IoCs all nested together. Who is actually in control depends on your perspective.

BTW, this is why some people don't like using Tomcat/Spring/Hibernate and prefer function composition. Because "who is in control" is obvious.

1

u/milkeater Dec 09 '20 edited Dec 09 '20

Oh no, not at all.

IoC is something that the Spring Framework was built around. Give control to the Framework and it will provide the wiring.

It was important for the testing aspects. Say I wanted to use a dependency inside a Service Layer object.

@Service
public class MyService {
   MyRepository repository;

   MyService(Repository repository){
      this.repository = repository;
   }
}

Because of this Inversion of Control, I only need to inform Spring of the root package and it will walk the tree finding all Classes and wiring them up creating instances of these objects as asked. It uses AOP that gives a "magic" type of behavior where things are happening behind the scenes without feeding you all the noise.

Now when you want to create a test for your Service logic:

@Mock 
MyRepository mockRepository;  //This is not real

MyService subject;

@Test
public void givenMyService_doWorkIsCalled_returnsThing(){
   MyObject expected = new MyObject();
   MyObject actual = subject.get(new MyObject());   

   when(mockRepository.get(any(MyObject.class))).thenReturn(expected);
   assertEquals(actual, expected);
}

Now our test doesn't need a real implementation with a connection to a real backend. In unit and functional tests, this can be faked and we will run hundreds or thousands of these tests in milliseconds. As we start actually exercising the connection to the database for performance or load-testing it will be during certain times aimed at certain environments for consistency. There is a test-pyramid now that tests more real aspects ensuring the world as we know it has not changed.

The biggest complaint I have seen is to your point: "What's happening here"

It's kind of like push-button start on your car. Someone who has only ever used a key to start a car (or screw-driver like my dad...j/k) would be confused. "What the hell did that?" Once you see it, it's no longer magical and becomes another non-event.

Getting there is a little disconnected similar to how async programming (and testing) can be. You can peel back the layers and see all the wiring providing your own Spring xml wiring. As far as Hibernate, Spring has an autoconfiguration where if it is aware of Data JPA it will automatically wire Hibernate up (assuming you've provided the dependency in gradle/maven) and wire up all the dependencies.

Spring handles all control. It is THE IoC container that handles Dependency Injection.

5

u/dartalley Intermediate Brewer Dec 09 '20

If you are only working on low scale applications then it really doesn't matter. Once you are working on any project that starts to get into millions to hundreds of millions of rows of data you will likely need to learn the ins and outs of the libraries you choose. Most libraries have many ways to provide hints and configuration options which can be crucial to the performance of your application.

Spring perfectly abstract database operations via JPA and we can handle every database operations without knowing what is in the background.

This is a pretty dangerous mindset. Your database, SQL, is the powerhouse of your application and often is significantly more important than the application code you write in Java. You should spend time learning SQL. Most of your major performance issues will live at the database layer or from using your abstraction in the wrong way (N+1 problem). Adding an index, optimizing a SQL query, or reducing the number of SQL queries you execute can easily give you 100x-1000x+ performance improvements.

1

u/milkeater Dec 09 '20

I disagree.

This is a pretty dangerous mindset. Your database, SQL, is the powerhouse of your application and often is significantly more important than the application code you write in Java.

The business logic happens inside your service layer in code. I believe the abstractions of Controller, Service, Persistence is a good rule to follow. There are more that may be added based on need (anti-corruption, access controls, etc) but inside the Service layer is where you would address time and space complexity.

The speak of "dangerous mindset" smacks loudly of a very gatekeeping speak of: "You must know before you touch". A form of gatekeeping that I find very unrealistic and has mired many enterprises into big balls of mud, fear of change, and indecision while software in the background continues to eat the world.

I don't believe in taking off the safety guards of the tools we use but actually adding more. Use unit, integration, load testing. Load testing is what addresses your concerns. If I run operations against a database that are quadratic or greater, I can write a test for this. If a regression occurs, let the consistent test be the gatekeeper.

I should be able to plug my persistence layer into SQL, PostGres, Cassandra without a re-write. Imagine having to install a new outlet for every electronic device you bought. They are equally foolish ideas and have consequences that often show up after years of complacency and daft oversight.

1

u/milkeater Dec 09 '20

Further point, as you mentioned indexing, normalization, and many other patterns for improving performance, obviously justified in certain circumstances.

But additionally CQRS, Distributed / Cloud based development, patterns that loosely couple.

BigO complexity speak is often premature in my experience. Whether you believe me or not, the words of God himself should carry some weight:

"Premature optimization is the root of all evil" ~ Donald Knuth

1

u/dartalley Intermediate Brewer Dec 09 '20

but inside the Service layer is where you would address time and space complexity.

Let's say I have a table called page views to track who visits which page in my application. This table has grown to 10 million rows. My dashboard showing top pages viewed over the past 30 days and their counts is running very slow now. Are you suggesting that this logic belongs in the service layer and I should return potentially several hundred thousand rows from the database over the network to then aggregate the data in java?

In this case, there is nothing you can do in the application layer short of writing your own database that would perform better than making sure this table is properly indexed and I have a decent SQL query (not counting caching). Simply adding the date index would significantly improve performance or going a step further if I have a composite index on (date, url) I can now write the query

select url, count(*) page_views 
from page_views 
where date between ? and ?
group by url
order by page_views desc LIMIT 10

I'm now hitting a covering index meaning SQL never even touches the raw data rows and pulls all of the data out of the index reducing its reads by half.

If I run operations against a database that are quadratic or greater, I can write a test for this. If a regression occurs, let the consistent test be the gatekeeper.

Let's say I have performance and integration tests and find out that my app is slow on some operations. If I rely soley upon the abstractions JPA in this case how would I go about addressing this? Realistically the fix could be adding an index in the db, optimizing the sql query because sometimes ORMs don't do the right thing, or possibly changing the config options or hints used by hibernate. If I only rely on JPA abstractions none of those options are available to me.

I should be able to plug my persistence layer into SQL, PostGres, Cassandra without a re-write.

This is what is taught in school but the reality tends to disagree. Even simply switching from one flavor of SQL to another is quite tedious. In order to gain performance, you often end up using dialect specific features mysqls insert ignore, insert on duplicate key update, mysqls views perform significantly worse than postgres, oracle, sql server. Every sql dialect has weird quirks you need to workaround.

Then you get into the even more fun ones.

What happens when you swap out a transactional database for a non-transactional database? Now you can end up with partial data in your system that the transactional database would have rolled back. Now you have to implement your own transaction logic or gracefully handle the partial data.

Maybe we decide to switch from SQL to Elasticsearch for a use case. Now not only do we lose transactions but we also lose real-time. Elasticsearch is a near real-time system meaning it can take up to 1 second for a document to be searchable after it is indexed. If I magically swap out my SQL implementation for Elasticsearch in my blog platform I could start throwing errors where I fail to find the post the user just submitted if the timing lines up.

Abstractions are nice but tend to limit what you have access to. When you care about performance and scale you have to understand your libraries and systems capabilities and how to utilize them effectively.

1

u/milkeater Dec 09 '20

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

I do not need the entire resultset and can lazyload as necessary although I will admit it is a common problem I had seen with junior developers. We solved that with education. If you saw I mentioned above Anti-Corruption layers can and do exist when necessary for integrity as well as CQRS patterns, migration patterns, etc.

Using JPA doesn't mean you should not index your RDBMS but if you believe the world is living and dying on RDBMS by itself, then I disagree.

Your passion is clear and respectable based on how thorough your response is but you are cherry picking. HTTP is a great example. We communicate to various underlying server implementations, Servlets, WSGI's, etc. Everyone speaks HTTP and the underlying implementation is transparent.

JPA, Repositories, DAO's is that to our database. I like the JPA interface to an extent for that abstraction reason. If you do need to pull the rip-cord you can and there is plenty of scenarios you can argue where that will make complete sense.

1

u/dartalley Intermediate Brewer Dec 09 '20

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

My argument would be this is significantly less performant. Even with pagination / lazy loading how much data are you sending over the network? How many rows did you force the database to read? How much data did you send over the network that you didn't even use. The simple SQL query with an aggregate function will be more performant and would use less resources on the database's side. It's doing less reads from disk/memory, it will use the connection for significantly less time freeing it up for other operations, it would use a trivial amount of extra CPU, the sql query is simple so it's likely less code than the java equivalent.

I'm a big fan of lukas eder and this is a fun but exaggerated talk related to pushing logic to the database. https://www.youtube.com/watch?v=wTPGW1PNy_Y

Everyone speaks HTTP and the underlying implementation is transparent.

This is actually a perfect example of why knowing your libraries is so important.

If I use a HTTP client abstraction Jersey, Retrofit, Feign, etc. All of these tend to use plugabble http cleints under the hood. Apache HTTP client, OkHttp, Grizzly client, etc.

But when I am using this abstraction are the following true?

  1. Is gzip enabled by default

  2. Is Keep Alive configured

  3. What are my default timeouts

  4. Is http caching implemented / enabled

  5. Is it using connection pooling or a connection per thread? If pooling how many max connections does it support by default?

More importantly, if I choose to swap my underlying implementation do all my settings stay the same or do I get all new defaults based on the new implementations defaults? All of these settings are very important at scale so you need to know if they are being properly set which usually involves configuring the underlying implementations. If you aren't large scale these are much less important.

You can write the cleanest, most performant, multithreaded java code using any fancy design patterns you want but if your HTTP client is configured to only have 2 concurrent HTTP connections to a single host your code will be slow until you change that setting.

It's impossible to be an expert with every tech your company uses but far too many engineers don't take the time to learn their libraries and database systems because they assume the abstractions will do everything for them. We should encourage engineers to learn the basics such as SQL and HTTP because they are transferrable to every programming language and any library, from there they should understand how their specific libraries interact with these systems / protocols.

1

u/milkeater Dec 09 '20

My argument would be this is significantly less performant. Even with pagination / lazy loading how much data are you sending over the network?

You would only search and return the exact rows requested. I encourage you to use it and see for yourself

The first method lets you pass an org.springframework.data.domain.Pageable instance to the query method to dynamically add paging to your statically defined query. A Page knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive (depending on the store used), you can instead return a Slice. A Slice knows only about whether a next Slice is available, which might be sufficient when walking through a larger result set.

Documentation

and it can be more performant should you have a less experienced dev who might do the classic with or grabbing the whole record set. If you have to order by, you are searching the entire thing anyways so would easily be able to do this with a Pageable.You are free to back up your claims with evidence. 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.

I said this somewhere else; Any time someone starts talking to me about optimization it's a warning sign to me that they are either very young or very old and not particularly close to the machinery, from Gods mouth to your ears

Pre-optimization is the root of all evil ~ Donald Knuth

1

u/dartalley Intermediate Brewer Dec 09 '20

You would only search and return the exact rows requested. I encourage you to use it and see for yourself

I am fully aware of how pagination and result sets work. Again given the example let's pretend there were 100k page views for the previous day.

select * from page_views where date = current_date - interval 1 day;

Assuming the table is indexed we will end up iterating the 100k index records for this date range. Next the database needs to read every record from the source table that returned from the index scan.

Assuming we use projections for a minor performance improvement

select url from page_views where date = current_date - interval 1 day;

Now we are lucky and can utilize our covering index so we only iterate the 100k values from the index and can return the results immediately without fetching the raw values from the table win!

Both scenarios we are still sending 100k rows back from the db paginated, streaming, however you want to do it.

Following this you will now need to count up all the results in java and finally sort the data at the end. This would be java code not sure where you are saying there would be 0 code in your other comment, granted it could be a one liner using streams apis.

In the aggregate query

select url, count(*) page_views 
from page_views 
where date = current_date - interval 1 day
group by url
order by page_views desc LIMIT 10

I am still hitting the covering index which means we only iterate the 100k index records and do not need to touch the raw rows. My query returns 10 rows instead of 100k rows (less data to transfer means my connection is freed up faster). I don't have to write any java code to sum the counts and sort them. This is a trivial amount of work for any SQL database.

I said this somewhere else; Any time someone starts talking to me about optimization it's a warning sign to me that they are either very young or very old and not particularly close to the machinery, from Gods mouth to your ears

You keep saying that but we are in fact discussing a problem where we said performance IS a problem... Secondly I'm using basic SQL knowledge and features to achieve a more performant result. I'd consider that using the right tool for the job instead of an "optimization". Nothing clever is going on, I'm not going out of my way to gain performance, it's a trivial sql query.

The reason SQL is awesome is because I don't have to think about performance, that is what the query plans and query optimizers are for. The SQL query plan will even change over time if the data set evolves and it knows a different plan will be faster. Or it might use one plan for one customer id and and a different plan for another customer id because it knows the cardinality of the data. Ignoring these extremely powerful features just because you want your business logic to only live in Java is a bit ignorant in my opinion but to each their own.

The video I linked above goes a little over the top but it's a great example.

1

u/milkeater Dec 09 '20

Incorrect.

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

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

The easiest way to prove it is show me a simple example. If what you are saying is true I shouldn't be able to poke a hole in your application correct? Let's move it to something concrete and stop talking past one another. I know SQL intimately, I don't think you know what I'm saying in Java though.

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.

1

u/milkeater Dec 09 '20

sql query is simple so it's likely less code than the java equivalent.

I can guarantee this is false unless your solution requires zero code as well.

1

u/LiteratureStriking Dec 09 '20

What happens when you swap out a transactional database for a non-transactional database?

This is a very good point. Relational databases are strongly consistent. But once you go non-transactional, you will have to design the system around eventual consistency. That goes beyond just swapping out an implementation, but rethinking your entire system architecture and user experience.

1

u/milkeater Dec 09 '20

This is a trivial FAANG interview question....been there, done that.

This sounds very ivory tower architect speak. I've moved a lot of work to the cloud where CAP has been solved.

This isn't some killer blow, just one more planning mechanism which has been dealt with in action (and plenty of times anger).

0

u/LiteratureStriking Dec 09 '20

You're just reiterating what I said, in kind of an obnoxious way.

Once you trade strong consistency for eventual consistency, you'll encounter issues that require planning to solve, which will involve rethinking the design. If your application is built around transactions, expecting isolation and atomicity, you're not going to have that anymore, which has significant implications. That's not ivory tower speak, that's just reality.

1

u/milkeater Dec 09 '20

I didn't reiterate it at all. I merely said you made a very obvious statement like: You need air to survive. It would be as if I said:

Well if you use multiple threads in Java you must be careful to synchronize when modifying state or use enums.

No shit.

1

u/LiteratureStriking Dec 09 '20

This isn't some killer blow, just one more planning mechanism which has been dealt with in action (and plenty of times anger).

You're reiterating what I said. I never said it was a killer blow. I am saying it is a planning mechanism. That plan will likely involve redesign.

Now maybe you know of some purely mechanical means of transforming an application built around transactions to one that will function the same without transactions, in which case not only me but the entire industry would be interested to hear about it.

1

u/milkeater Dec 09 '20

To be clear, are you telling me if I asked you to migrate a system, let's say on-prem JBoss tied to MySQL to a system in AWS where the system and also DB may scale up or down following CQRS patterns during peak utilization. The host may change, nothing is dedicated.

Is this something you are telling me you couldn't solve?

If not I'm going to recommend you checking out some good resources that I used quite some time ago as I was learning the material. This magic has been solved in hardware and software.

System Design Primer

1

u/LiteratureStriking Dec 09 '20

Is this something you are telling me you couldn't solve?

No, I am not saying that.

I am saying that it isn't trivial and would require rethinking the system design, for example migrating the system design to CQRS. Many applications might start off without separate read models and write models or clear separation between commands and queries, which would require analysis about the data flow of the entire system. This is entirely doable, but not at all trivial.

You might end up pushing JPA into the write model (since the session deals with tracking mutation), and push native queries to the read model (or views or materialized views on the DB) or to non-relational stores like Cassandra (with some machinery in the middle to keep the data stores in sync).

→ More replies (0)

0

u/LiteratureStriking Dec 09 '20

I should be able to plug my persistence layer into SQL, PostGres, Cassandra without a re-write.

You're talking about a level of abstraction way higher than JPA. Obviously you should hide details about where your objects come from through a Repository or the like. In fact, your business logic shouldn't even be concerned with JPA.

But if you are using JPA, you can't ignore SQL. That is a dangerous mindset. To a limited extent it is possible to swap database vendors if you are doing extremely simple CRUD. But, optimizing performance will require understanding how JPA generates SQL and possibly adjusting the domain model. Or, you'll have to pop the escape hatch and write native SQL queries, which will destroy portability.

1

u/milkeater Dec 09 '20

I think that's my point.

In using JPA I can specify entities via their relationships. If I need to customize JPA I will to an extent but as soon as custom queries or native SQL come in, there is a code smell to me. Am I keeping legacy code alive because it's generating revenue, sure we'll go there.

It's possible I wasn't being clear but for a junior dev I'd give them a starting model of:

Controller -> Service -> Persistence -> DB

The ORM between Persistence and DB is where JPA cares. JPA is interface, hibernate is implementation. I think you know this but you identified the wrong layer so clarifying that we are both speaking the same language.

I typically encourage Spring RestController for concerning with translation of net communication, Security and handing off DO's to my Service. Service performs whatever work and leverages Repositories where JPA will do the handoff from DO's to Entities and map tables.

Whether I plug in to Cassandra or MySQL, JPA and Spring will sort that out via the Data Source I feed it.

Maybe you are unfamiliar but JPA to various underlying DB is a fairly established thing: https://dzone.com/articles/treat-your-data-as-an-object-using-kundera

1

u/LiteratureStriking Dec 09 '20 edited Dec 09 '20

If I need to customize JPA I will to an extent but as soon as custom queries or native SQL come in, there is a code smell to me.

To put it simply, JPQL is good for OLTP operations, when you are fetching and updating only a few records with very narrow criteria (where mutation tracking in the session is useful). This is one reason ORMs are very good for write models (in CQRS).

If you're bulk fetching to populate a read-only view, fetching entities only needlessly bloats the session (why track what you're not going to change?). You can make the entity @Immutable (which is Hibernate specific), so that Hibernate turns off mutation checking. Or, you can use JPQL to do a DTO projection. However, these options only make sense as long as you stay close to simple CRUD that can be modeled by entity relations.

Once your data retrieval starts leaning more towards OLAP, and requiring actual analytic functions, JPQL is not going to help, and you're going to have to use native database features. Or, you have to load data unnecessarily into the application to use Java for analytics, which is just extremely wasteful. The goal should be to minimize the amount of data transferred from the database to the application.

So you can either use a native query, use Hibernate-specific features like @Formula to map native SQL into the entity model, or you can use a view on the DB. However, in all of these cases you're now tied to the DB, and your application is no longer portable.

I think you know this but you identified the wrong layer so clarifying that we are both speaking the same language.

I am specifically talking about a pattern like this:

Controller -> Service -> Repository <- JpaRepository <- DB

The Repository is a high-level interface defined in application terms. You can use JPA to implement the Repository. But, if you define the Repository interface in high-level enough terms, you can replace the implementation with one that uses Cassandra or a web service.

I would not use JPA with Cassandra. I would rather use Spring Data Cassandra, which provides a convenient wrapper over the native driver. Cassandra is best used for highly tailored denormalized views and joins are extremely expensive, so I don't see how ER mapping makes any sense.

1

u/milkeater Dec 09 '20

I think you have a style of your own that you enjoy. Not necessarily something I typically encounter or encourage but you clearly understand the tradeoffs.

I feel I understand your point and appreciate the conversation and clear effort you've put into being thorough.

As a note, the standard pattern is to extend the JpaRepository with an interface and allow Spring to provide the implementation.

So something more concrete would be

RestController -> Service -> JpaRepository -> RDBMS DataSource with JDBC

I am familiar with Cassandra but if you are doing legacy work or the edge case type of behaviors that you seem to have to focus on for your role, migrating something that implements JPA to another backend would likely be unnecessary to rewrite, the argument was more around portability and the fact this has been in existence for a considerable amount of time. If performance is your concern I would test it first and then have a pretty obvious CBA. If the costs of rewriting the wiring to persistence vs the value it provides, the decision is easy to make (and argue) and nobody needs to lose their bonus ;)

1

u/LiteratureStriking Dec 09 '20

That is the standard pattern of Spring Data repositories, which I don't agree with. It makes for more restrictive semantics to fit the way Spring Data generates the implementation. I prefer to define the implementation in terms of the application, which is how the pattern is described in Domain Driven Design.

migrating something that implements JPA to another backend would likely be unnecessary to rewrite

This is something I frequently do, for example pinching out microservices from monolithic applications. Or, improving read performance by using alternate data models (for example using Cassandra for datacenter affinity to improve responsiveness, using jobs to push data from the RDBMS in another datacenter).

Well defined interfaces between modules open up many more paths to platform evolution.

1

u/milkeater Dec 09 '20

That is the standard pattern of Spring Data repositories, which I don't agree with. It makes for more restrictive semantics to fit the way Spring Data generates the implementation.

I disagree. I feel you are misunderstanding the instrumentation. Feel free to prove your point by example.

1

u/LiteratureStriking Dec 09 '20 edited Dec 09 '20

This is from experience using Spring Data Repositories.

You have a couple of options:

public interface FooRepository extends CrudRepository<Foo, Long> {
    Foo findById(int id);
}

Spring Data can analyze the method name and generate an implementation.

public interface FooRepository extends CrudRepository<Foo, Long> {
    @Query("select f from Foo where id = ?1")
    Foo findById(int id);
}

Spring Data will create an implementation based on @Query.

And there's way to mix custom implementations with the generated ones with an additional interface that has the custom methods and a separate implementation class, which I'm not going to bother providing an example for.

And Spring Data will create an implementation of the interface, basically an enhanced mechanism that Spring uses for creating any type of proxy bean.

I thought this was the coolest thing in the world. But I found that I was designing methods that were directly mapping to JPQL queries, so that the interfaces started looking kind of like named queries, but as interface methods instead of a string. The Repository ended up looking like a DAO.

The idea behind dependency inversion is that interface should serve the application, not the persistence technology. I didn't like that my abstractions were getting so closely tied to Spring Data and there was too much magic. Magic on top of magic on top of magic. So I ditched it.

1

u/milkeater Dec 09 '20

Gonna be honest, you are talking to me about the 101 of Spring Data....I think there are a few layers deeper that you could benefit from but like I said earlier, if what you are doing works for you more power to you.

Not a way I would approach it. If you want to share a repo on an app with the problem you are trying to articulate, I can maybe show you more clearly the modifications I would make to simplify without this excess.

2

u/feral_claire Software Dev Dec 09 '20

Yes you are using hibernate so you need to know it. (since you mentioned using spring boot jpa, that's going to be hibernate by default). The abstraction makes it seem like you can get away without much knowledge, and wet simple cases and tutorials you can, but the reality is you need to know what's going on under the hood or it so come to bite you.

Springs JPA abstraction makes it far and easy to write the code, but it's not an excuse for not understanding it.

1

u/uuuuuhhhh69 Dec 10 '20

If you are looking for a job doing java development, then I would recommend learning it. It’s on the required list of most job descriptions, and having it on your resume pleases the resume scanners most companies use. It’s also good for academic purposes though.

-7

u/wildjokers Dec 09 '20

I really never had know about Hibernate because Spring perfectly abstract database operations via JPA

It really concerns me that you don’t realize that JPA is just a specification and that Hibernate is an implementation of that specification (hibernate predates JPA but that isn’t important). There are other JPA implementations out there but hibernate is most likely the one you are using.

You absolutely must know hibernate since that is probably what you are using. I am actually dumbfounded by your question and just shook my head in utter dismay when I read it.

You just have to know what hibernate is doing for you and you absolutely have to keep on eye on the SQL it generates. You also need to be writing JPQL for your read-only queries and absolutely do not do read-only queries with entities. It sounds like you are in for a very rude awakening regarding DB performance issues.

How can you possibly think you can just ignore knowing how the data layer is accessed in your app when the data layer is by far the performance bottleneck in a large majority of apps?

This question quite honestly irritated the shit out of me and I think I have lost all hope in the new generation of developers.

4

u/anagrammatron Dec 09 '20

Chill, it's r/javahelp, it's alright to ask questions here without getting lectured by neckbeards.

0

u/[deleted] Dec 09 '20

[deleted]

2

u/milkeater Dec 09 '20

You miss the point of Boot.

80% of software written in the enterprise could be written using boot meaning you can have a web server in 5 lines of code. It is as templatized and modular a framework that can exist. An even more powerful argument is the underlying inversion of control, DI, testability.

I tell you about the object I want to use, and you find it and wire it up recursively. There is a threshold of complexity in every framework where you go beyond the 80% use-case and get into your "under the hood" scenario. Sometimes finding yourself there in the first place is a code smell and something you should question, sometimes a necessary evil.

The greatest power is this on-ramp into Spring for junior devs and not eating the whole cake in one bite to be productive. They have guardrails to learn the sharp edges of enterprise dev and you can control it applying your own layers (autologging, audit, security) using AOP without overtly getting in their face and beating them with Ivory Tower hammers.

0

u/[deleted] Dec 09 '20

[deleted]

2

u/milkeater Dec 09 '20

Don't think of me as trying to make that as a point of authority. 70% of all statistics are made up, but I'm calling from the 80/20 reference if you are familiar.

Spring Boot is merely a few annotations. It is an alternative to providing the xml mapping. The complaint was Spring is verbose in it's setup, Boot just walks the dependency graph and wires for you. Spring Boot is not a: "You are too small to ride Spring, use this toy."

I have written software for a respectable amount of the top 100 in addition to coming into escalation support issues where $100k's were being lost per minute. There's a certain level of scrutiny in the RCA at that level. Because of that I don't feel uncomfortable with making the above statements although I'm not saying it's the only opinion and always encourage "arguing" or "debate". It's healthy and sharpens the conversation.

Source: Worked for and sold Spring.