开发者

How do you see a good Spring+Hibernate DAO module design?

开发者 https://www.devze.com 2022-12-23 13:06 出处:网络
First, we create classes that represent db entities, ok, done. Let\'s say we use Hibernate session factory and JPA annotations.

First, we create classes that represent db entities, ok, done. Let's say we use Hibernate session factory and JPA annotations.

Now we must creat开发者_StackOverflowe a DAO: getUserById, getAllUsers() etc.

What do you recommend about transaction management, session factory, how a good design to be made?


  1. Make the DAO generic. See the Don't repeat the DAO article.
  2. Transaction management should be spring-managed. Use a JpaTransactionManager. Transactions can be marked in two ways, and they should mark methods of the service classes, not the DAO:
    • using @Transactional on each transactional method (in combination with <tx:annotation-driven /> in applicationContext.xml)
    • using <tx:advice> and the appropriate <aop:config>
  3. Use OpenEntityManagerInViewFilter or OpenEntityManagerInViewInterceptor in order to avoid LazyInitializationException

Read this for more details.

0

精彩评论

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