-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHibernateConnection.java
62 lines (57 loc) · 2.51 KB
/
HibernateConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package database;
import java.sql.Connection;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/** êëàññ, êîòîðûé ñîçäàåò Hibernate.Session íà îñíîâàíèè Connection */
public class HibernateConnection {
private SessionFactory sessionFactory=null;
/** ñîçäàòü íà áàçå Connection Hibernate íàäñòðîéêó
* @param hibernateDialect - "org.hibernate.dialect.FirebirdDialect"
* ( example for HSQLDB - org.hsqldb.jdbcDriver)
* @throws âûáðàñûâàåò èñêëþ÷åíèå â ñëó÷àå, êîãäà íå óäàëîñü ñîçäàòü Hibernate
* */
public HibernateConnection(String hibernateDialect,Class<?> ... classes) throws Exception{
sessionFactory=getAnnotationConfiguration(hibernateDialect,classes).buildSessionFactory();
}
/** ñîçäàòü íà áàçå Connection Hibernate íàäñòðîéêó
* @param hibernateDialect - "org.hibernate.dialect.FirebirdDialect"
* @throws âûáðàñûâàåò èñêëþ÷åíèå â ñëó÷àå, êîãäà íå óäàëîñü ñîçäàòü Hibernate
*
public HibernateConnection(Connection connection, String hibernateDialect,Class<?> ... classes) throws Exception{
this.connection=connection;
sessionFactory=getAnnotationConfiguration(hibernateDialect,classes).buildSessionFactory();
}
*/
private AnnotationConfiguration getAnnotationConfiguration(String hibernateDialect,Class<?> ... classes){
AnnotationConfiguration aconf = new AnnotationConfiguration();
Properties properties=new Properties();
properties.put("hibernate.dialect", hibernateDialect);
//properties.put("hibernate.connection.driver_class", "org.firebirdsql.jdbc.FBDriver");
//properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:baseball");
//properties.put("hibernate.connection.username", "sa");
//properties.put("hibernate.connection.password", "");
properties.put("hibernate.connection.pool_size", "10");
properties.put("hibernate.connection.autocommit", "false");
//properties.put("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
//properties.put("hibernate.hbm2ddl.auto", "create-drop");
// properties.put("hibernate.show_sql", "true");
aconf.setProperties(properties);
for(int counter=0;counter<classes.length;counter++){
aconf.addAnnotatedClass(classes[counter]);
}
return aconf;
}
/** get Hibernate Session */
public Session openSession(Connection connection){
return sessionFactory.openSession(connection);
}
public void close(){
//System.out.println("close");
this.sessionFactory.close();
}
public void finalize(){
this.close();
}
}