跳到主要內容

發表文章

目前顯示的是 3月, 2009的文章

使用Spring的HibernateDAOSupport可能遇到的Lazy initialization問題

在專案中我們使用DAO這個pattern來封裝資料庫存取層,Hibernate Quickly (Manning)中建議我們可以繼承Spring所提供的HibernateDAOSupport來簡化DAO程式碼,這是很常見的作法。 原本method重複性很高的部份: public void create(Event event) throws DataAccessLayerException { try { startOperation(); session.save(event); tx.commit(); } catch (HibernateException e) { handleException(e); } finally { HibernateFactory.close(session); } } 可以被簡化為: public abstract class AbstractSpringDao extends HibernateDaoSupport{ public AbstractSpringDao() { } protected void saveOrUpdate(Object obj) { getHibernateTemplate().saveOrUpdate(obj); } protected void delete(Object obj) { getHibernateTemplate().delete(obj); } ... } 都透過Sprgin提供的HibernateTemplate幫我們作掉那些重複的開、關session, transaction動作。 以下擷取自Spring原始碼 package org.springframework.orm.hibernate3; ... public class HibernateTemplate extends HibernateAccessor implements HibernateOperations { ... public Object execute(HibernateCallback action) throws DataAccessException { Session session = (!this.a

一個Web Application系統架構 (Spring + Struts + Hibernate)

近期使用這三套framework作系統,系統架構如下: 其中使用的Design Pattern: MVC: To separate core business model functionality from the presentation and control logic that uses this functionality. Such separation allows multiple views to share the same enterprise data model, which makes supporting multiple clients easier to implement, test, and maintain. Front Controller Use a controller as the initial point of contact for handling a request. The controller manages the handling of the request, including invoking security services such as authentication and authorization, delegating business processing, managing the choice of an appropriate view, handling errors, and managing the selection of content creation strategies. DAO(Data Access object) To abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data. 如果同時運用這三個application framework,這樣的架構算是很常見。 Pros: 運用struts前端控制器(front controller)來集中管理頁面導引 Struts custom tag來強化 JSP的不足 用Sp