KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > hibernate > HibernateCar


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

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 /**
32  *
33  * @author Herkules
34  */

35 public class HibernateCar extends Car
36 {
37     
38     private SessionFactory mFactory;
39     
40     private final String JavaDoc mDBType;
41     
42     private Session mSession;
43     
44     
45     public HibernateCar(String JavaDoc dbType){
46         mDBType = dbType;
47     }
48
49     public String JavaDoc 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     /**
87      *
88      */

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 JavaDoc ex ) {
102                 ex.printStackTrace();
103             }
104             
105             cfg.setProperty("hibernate.connection.url", Jdbc.settings().getConnectUrl( mDBType ));
106             
107             String JavaDoc user = Jdbc.settings().getUsername( mDBType );
108             if(user != null){
109                 cfg.setProperty("hibernate.connection.user", user);
110             }
111             
112             String JavaDoc password = Jdbc.settings().getPassword( mDBType );
113             if(password != null){
114                 cfg.setProperty("hibernate.connection.password", password);
115             }
116             
117             String JavaDoc 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