1 19 20 package org.polepos.teams.hibernate; 21 22 import org.polepos.framework.*; 23 import org.polepos.teams.hibernate.data.*; 24 import org.polepos.teams.jdbc.*; 25 26 import net.sf.hibernate.*; 27 import net.sf.hibernate.Session; 28 import net.sf.hibernate.cfg.*; 29 import net.sf.hibernate.tool.hbm2ddl.*; 30 31 35 public class HibernateCar extends Car 36 { 37 38 private SessionFactory mFactory; 39 40 private final String mDBType; 41 42 private Session mSession; 43 44 45 public HibernateCar(String dbType){ 46 mDBType = dbType; 47 } 48 49 public String name(){ 50 return mDBType; 51 } 52 53 public void openSession() throws CarMotorFailureException{ 54 55 if ( mFactory == null) { 56 mFactory = getSessionFactory(); 57 } 58 59 assert mSession == null; 60 61 try { 62 mSession = mFactory.openSession(); 63 } catch (HibernateException e) { 64 e.printStackTrace(); 65 throw new CarMotorFailureException(); 66 } 67 } 68 69 public void closeSession(){ 70 71 if(mSession != null){ 72 try { 73 mSession.close(); 74 } catch (HibernateException e) { 75 e.printStackTrace(); 76 } 77 mSession = null; 78 } 79 80 } 81 82 public Session getSession(){ 83 return mSession; 84 } 85 86 89 private SessionFactory getSessionFactory() 90 { 91 try 92 { 93 Configuration cfg = new Configuration() 94 .addClass( HibernatePilot.class ) 95 .addClass( HibernateTree.class ) 96 .addClass( HibernateIndexedPilot.class ) 97 .addClass(HB0.class); 98 99 try{ 100 Class.forName( Jdbc.settings().getDriverClass( mDBType ) ).newInstance(); 101 } catch ( Exception ex ) { 102 ex.printStackTrace(); 103 } 104 105 cfg.setProperty("hibernate.connection.url", Jdbc.settings().getConnectUrl( mDBType )); 106 107 String user = Jdbc.settings().getUsername( mDBType ); 108 if(user != null){ 109 cfg.setProperty("hibernate.connection.user", user); 110 } 111 112 String password = Jdbc.settings().getPassword( mDBType ); 113 if(password != null){ 114 cfg.setProperty("hibernate.connection.password", password); 115 } 116 117 String dialect = Jdbc.settings().getHibernateDialect( mDBType ); 118 if(dialect != null){ 119 cfg.setProperty("hibernate.dialect", dialect); 120 } 121 122 cfg.setProperty("hibernate.query.substitutions", "true 1, false 0, yes 'Y', no 'N'"); 123 cfg.setProperty("hibernate.connection.pool_size", "1"); 124 cfg.setProperty("hibernate.proxool.pool_alias", "pool1"); 125 cfg.setProperty("hibernate.jdbc.batch_size", "0"); 126 cfg.setProperty("hibernate.jdbc.batch_versioned_data", "true"); 127 cfg.setProperty("hibernate.jdbc.use_streams_for_binary", "true"); 128 cfg.setProperty("hibernate.max_fetch_depth", "1"); 129 cfg.setProperty("hibernate.cache.region_prefix", "hibernate.test"); 130 cfg.setProperty("hibernate.cache.use_query_cache", "true"); 131 cfg.setProperty("hibernate.cache.provider_class", "net.sf.hibernate.cache.EhCacheProvider"); 132 133 cfg.setProperty("hibernate.proxool.pool_alias", "pool1"); 134 cfg.setProperty("hibernate.proxool.pool_alias", "pool1"); 135 136 137 138 139 140 141 SessionFactory factory = cfg.buildSessionFactory(); 142 new SchemaExport(cfg).create(true, true); 143 return factory; 144 } 145 catch ( MappingException mex ) 146 { 147 mex.printStackTrace(); 148 } 149 catch ( HibernateException hex ) 150 { 151 hex.printStackTrace(); 152 } 153 return null; 154 } 155 156 } 157 | Popular Tags |