1 package com.opensymphony.webwork.hibernate; 2 3 import com.opensymphony.util.ClassLoaderUtil; 4 import com.opensymphony.xwork.interceptor.component.Disposable; 5 import com.opensymphony.xwork.interceptor.component.Initializable; 6 import com.opensymphony.xwork.interceptor.component.ResourceAware; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.cfg.Configuration; 9 import org.hibernate.tool.hbm2ddl.SchemaUpdate; 10 11 import java.net.URL ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 22 public class HibernateConfigurationImpl implements HibernateConfiguration, 23 Initializable, Disposable, ResourceAware { 24 SessionFactory sessionFactory; 25 Set resources = new HashSet (); 26 27 public void init() { 28 System.out.println("HibernateConfiguration.init"); 29 URL url = ClassLoaderUtil.getResource("hibernate.cfg.xml", getClass()); 30 Configuration configuration = new Configuration().configure(url); 31 32 resources.add("hibernate.cfg.xml"); 33 for (Iterator iterator = configuration.getImports().entrySet().iterator(); iterator.hasNext();) { 34 Map.Entry entry = (Map.Entry ) iterator.next(); 35 String name = (String ) entry.getKey(); 36 resources.add(name.replaceAll("\\.", "/") + ".hbm.xml"); 37 } 38 39 sessionFactory = configuration.buildSessionFactory(); 40 new SchemaUpdate(configuration).execute(true, true); 41 } 42 43 public Set getDependentResources() { 44 return resources; 45 } 46 47 public SessionFactory getSessionFactory() { 48 return sessionFactory; 49 } 50 51 public void dispose() { 52 System.out.println("HibernateConfiguration.dispose"); 53 sessionFactory.close(); 54 } 55 } 56 | Popular Tags |