Java – Dao and service layer in spring: session management
Do I correctly understand Dao & amp; Principle of service layer interconnection?
The service layer uses the DAO object and can invoke more than one DAO method in a function. Therefore, the service layer must:
>Instantiate Dao implementation object > call multiple methods of Dao as needed
If Dao implements an interface, does Dao interface have to have a method setsessionfactory()?
How to declaratively tag in spring:
>Dao Objects > service layer methods and classes as a whole
So it can provide what you need?
Solution
For my project, I wrote a base class with a setsessionfactory () method for all my Dao extensions Then I connect my Dao with spring so that it injects sessionfactory into each Dao
Hibernate has a sessionfactory Getcurrentsession(), so if you inject sessionfactory into Dao and use this method, the scope of session will be defined according to your transaction management mechanism
This means that if you have such a method:
@Transactional public void doSomething(){ dao1.makeCall(); dao2.makeOtherCall(); }
The sessionfactory injected into each Dao at construction time will use the same session But only limited to the scope of the transaction