-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHibernateConnection.java
executable file
·52 lines (47 loc) · 2.03 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
package gui.editor.database;
import gui.editor.database.wrap.PayKind;
import gui.editor.database.wrap.Tariff;
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;
/** ñîçäàòü íà áàçå Connection Hibernate íàäñòðîéêó
* @param hibernateDialect - "org.hibernate.dialect.FirebirdDialect"
* @throws âûáðàñûâàåò èñêëþ÷åíèå â ñëó÷àå, êîãäà íå óäàëîñü ñîçäàòü Hibernate
* */
public HibernateConnection(String hibernateDialect) throws Exception{
//Connection connection
//DatabaseMetaData data=connection.getMetaData();
//System.out.println(data.getDriverName());
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", "1");
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);
aconf.addAnnotatedClass(PayKind.class);
aconf.addAnnotatedClass(Tariff.class);
sessionFactory=aconf.buildSessionFactory();
}
public Session openSession(Connection connection){
return sessionFactory.openSession(connection);
}
public void close(){
System.out.println("close");
this.sessionFactory.close();
}
public void finalize(){
this.close();
}
}