跳到主要內容

發表文章

目前顯示的是有「hibernate」標籤的文章

java.util.Date存到datetime欄位會遺失時間資訊

使用Hibernate , SQL Server 2008將 Date存到datetime欄位中會遺失時間資訊,只剩下日期。如下:          2010-11-02 00:00:00.000 原有設定: bean-hbm.xml                             需使用sql-type 來複寫Hibernate default mapping。 The sql-type attribute allows the user to override the default mapping of a Hibernate type to SQL datatype. (hibernate reference 3.3.2 GA)                            

Unidirectional one-to-many foreign key自動插入

在使用單向一對多mapping,一種簡單的作法是在「many」的一方中的table中的某一個欄位用來儲存「one」一方的主鍵,如果以下方範例來說, Address表中有一欄叫personId是Person表中的主鍵。 create table Person ( personId bigint not null primary key ) create table Address ( addressId bigint not null primary key, personId bigint not null ) 取自Hibernate reference Documentation 3.3.2.GA, 7.2.3 set 要加上 cascade="all" or cascade="save-update" 當我們在儲存Person物件(含有Address)的時候,hibernate可以自動幫我們將Person的主鍵填入Address表的personId欄位中而不需要手動寫程式。 關鍵在Person的mapping document上 key column中 not-null="true" 的這個設定。 public class OneToMany { public static void main(String[] args){ Address add1 = new Address(); Address add2 = new Address(); Person p1 = new Person(); p1.sets(new HashSet()); p1.getAddresses().add(add1); p1.getAddresses().add(add2); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(p1); // cascade tx.commit(); ...

使用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...