Lazy Initialization and the DAO pattern with Hibernate and Spring (2)
2010-11-06 17:14
561 查看
Being Lazy in the Business Layer
Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let's run through a quick example. Suppose we have an interface BusinessObject:
Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it's methods to lazily access data objects. Take a look at the fragment below:
When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.
Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let's run through a quick example. Suppose we have an interface BusinessObject:
public interface BusinessObject { public void doSomethingThatInvolvesDaos(); } The class BusinessObjectImpl implements BusinessObject: public class BusinessObjectImpl implements BusinessObject { public void doSomethingThatInvolvesDaos() { // lots of logic that calls // DAO classes Which access // data objects lazily } }
Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it's methods to lazily access data objects. Take a look at the fragment below:
<beans> <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl"> <property name="someDAO"><ref bean="someDAO"/></property> </bean> <bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"><ref bean="businessObjectTarget"/></property> <property name="proxyInterfaces"> <value>com.acompany.BusinessObject</value> </property> <property name="interceptorNames"> <list> <value>hibernateInterceptor</value> </list> </property> </bean> </beans>
When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.
相关文章推荐
- [转]Lazy Initialization and the DAO pattern with Hibernate and Spring
- Lazy Initialization and the DAO pattern with Hibernate and Spring(1)
- Lazy Initialization and the DAO pattern with Hibernate and Spring (3)
- Lazy Initialization and the DAO pattern with Hibernate and Spring(Spring 与Hibernate的延迟加载和Dao模式)
- Lazy Initialization and the DAO pattern with Hibernate and Spring
- The limitation and reason of EWF with a Hibernate Once/Resume Many Configuration
- org.springframework.dao.DuplicateKeyException: a different object with the same identifier value....
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
- The API Gateway Pattern: Angular JS and Spring Security Part IV
- IntelliJIDEA Getting+Started+with+Spring+MVC,+Hibernate+and+JSON
- org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was
- 基于Spring+Hibernate+DAO的hibernate的lazy的实现.
- It will cause a new session when you use lazy loading collection, and the collection will assicated with the new session and the
- the design principles and code analysis of hibernate lazy loading
- Spring with Hibernate persistence and transactions
- Flex 2.0 @ Work in combination with Spring and Hibernate
- 出现org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed问题以及解决办法
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource
- The Practise of Spring MVC with Hibernate
- [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework