<class name="mypack.Customer" table="CUSTOMERS" lazy="true">
tx = session.beginTransaction(); Customer customer=(Customer)session.load(Customer.class,new Long(1)); customer.getName(); tx.commit();
select * from CUSTOMERS where ID=1; select * from ORDERS where CUSTOMER_ID=1;
ERROR LazyInitializer:63 - Exception initializing proxy net.sf.hibernate.ObjectNotFoundException:
No row with the given identifier exists: 1, of class: mypack.Customer
tx = session.beginTransaction(); Customer customer=(Customer)session.load(Customer.class,new Long(1)); tx.commit(); session.close(); customer.getName();
ERROR LazyInitializer:63 - Exception initializing proxy net.sf.hibernate.HibernateException:
Could not initialize proxy - the owning Session was closed
tx = session.beginTransaction(); Customer customer=(Customer)session.load(Customer.class,new Long(1)); if(!Hibernate.isInitialized(customer)) Hibernate.initialize(customer); tx.commit(); session.close(); customer.getName();
tx = session.beginTransaction(); Customer customer=(Customer)session.load(Customer.class,new Long(1)); customer.getId(); tx.commit(); session.close(); customer.getName();
ERROR LazyInitializer:63 - Exception initializing proxy net.sf.hibernate.HibernateException:
Could not initialize proxy - the owning Session was closed
<beans> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor"/> </list> </property> <property name="mappings"> </bean> <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"><ref bean="sessionFactory"/></property> </bean> </beans>
<web-app> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*. spring </url-pattern> </filter-mapping> </web-app>
public class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO { public Product getProduct(Integer productId) { return (Product)getHibernateTemplate().load(Product.class, productId); } public Integer saveProduct(Product product) { return (Integer) getHibernateTemplate().save(product); } public void updateProduct(Product product) { getHibernateTemplate().update(product); } }
public interface BusinessObject{ public void doSomethingThatInvolvesDaos(); }
// 類BusinessObjectImpl實現了BusinessObject接口: public class BusinessObjectImpl implements BusinessObject{ public void doSomethingThatInvolvesDaos(){ // lots of logic that calls // DAO classes Which access // data objects lazily } }
<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>
public abstract class MyLazyTestCase extends TestCase { private SessionFactory sessionFactory; private Session session; public void setUp() throws Exception { super.setUp(); SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory"); session = SessionFactoryUtils.getSession(sessionFactory, true); Session s = sessionFactory.openSession(); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s)); } protected Object getBean(String beanName) { //Code to get objects from Spring application context } public void tearDown() throws Exception { super.tearDown(); SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); Session s = holder.getSession(); s.flush(); TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory); } }