开发者

use of @org.springframework.transaction.annotation.Transactional outside of the DAO layer?

i have a quite classical issus : Access Lazy Collection outside of the DAO, witch lead to LazyInitializationException.开发者_StackOverflow中文版

Is that a clean and reasonable solution to use @Transactional one some method of the service, outside of the DAO, who use these lazy collection ?

Thanks


Yes it is. The one who knows which tasks must be a unit of work (a Transaction) is the service, because it is the one who "knows" the business.


Neuquino has the right answer, but it's not very clear and understandable, so I'll try to rephrase it:

You usually want one transaction for one service method, which is why you annotate the service layer with @Transactional, not the DAO layer. (The nice thing about the Spring @Transactional annotation is that it is independent of the underlying implementation, so there should usually be no code in the service layer that's specific to your persistence layer, e.g. no calls to hibernate Session or JPA EntityManager ). When unit testing DAOs, put the @Transactional annotations on the test methods (your tests are services, not DAOs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜