What is the correct way to use spring MVC with Hibernate in DAO, service level architecture

I am using Spring MVC with Hibernatedaosupport for my DAO classes. Confuses here where to start the transaction, should it be at the service level or at the DAO level?

My view interacts with the Service layer. DAOs are introduced into services.

What is the correct way to use Spring MVC with Hibernate in a DAO, service level architecture?

+8
spring spring-mvc architecture hibernate dao
source share
3 answers

IMHO transactions should go to the service level. Typically, a single business transaction consists of several requests and updates. If you place @Transactional only at the DAO level, each request and update will be performed in a separate transaction, which effectively defeats the purpose of the transaction.

But if the services are @Transactional , each database interaction connects to one main transaction when the web tier enters the service tier. Please note that in this case, if the web layer launches several maintenance methods, each of them will be launched in a separate transaction (the same problem moves up one level). But placing @Transactional in a web layer can lead to unexpected side effects, such as the N + 1 issue, that would be caught otherwise. Thus, try saving one business transaction in a single service method called from the web tier.

+20
source share

Obviously a DAO layer. Everything that connects to the data access layer should be in the DAO layer and (preferably) annotated with @Repository, and your service (annotated with @Service) should have a DAO instance handle.

A service can invoke multiple DAOs, but not vice versa. DAO objects must be atomic in nature.

If you start a transaction, then it should be at the service level, in my opinion, which supports my previous expression, where I mention that DAOs must be atomic in nature.

0
source share

There is complete information for service levels, DAO level, entities and controllers. It has a complete tutorial with a brief description for each module.

Website: Spring MVC with Hibernate CRUD

Or you can visit the YouTube channel: Spring MVC with Hibernate CRUD VIDEO

0
source share

All Articles