1 16 package com.ibatis.dao.engine.transaction.hibernate; 17 18 import com.ibatis.common.resources.Resources; 19 import com.ibatis.dao.client.DaoException; 20 import com.ibatis.dao.client.DaoTransaction; 21 import com.ibatis.dao.engine.transaction.DaoTransactionManager; 22 import net.sf.hibernate.SessionFactory; 23 import net.sf.hibernate.cfg.Configuration; 24 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 28 public class HibernateDaoTransactionManager implements DaoTransactionManager { 29 30 private SessionFactory factory; 31 32 public void configure(Properties properties) { 33 try { 34 Configuration config = new Configuration(); 35 36 Iterator it = properties.keySet().iterator(); 37 while (it.hasNext()) { 38 String key = (String ) it.next(); 39 String value = (String ) properties.get(key); 40 if (key.startsWith("class.")) { 41 config.addClass(Resources.classForName(value)); 42 } 43 } 44 45 Properties props = new Properties (); 46 props.putAll(properties); 47 config.setProperties(props); 48 49 factory = config.buildSessionFactory(); 50 51 } catch (Exception e) { 52 throw new DaoException("Error configuring Hibernate. Cause: " + e); 53 } 54 } 55 56 public DaoTransaction startTransaction() { 57 return new HibernateDaoTransaction(factory); 58 } 59 60 public void commitTransaction(DaoTransaction trans) { 61 ((HibernateDaoTransaction) trans).commit(); 62 } 63 64 public void rollbackTransaction(DaoTransaction trans) { 65 ((HibernateDaoTransaction) trans).rollback(); 66 } 67 } 68 | Popular Tags |