-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHibernateConfig.java
More file actions
79 lines (48 loc) · 2.74 KB
/
HibernateConfig.java
File metadata and controls
79 lines (48 loc) · 2.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.doing.more.java.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateConfig
{
private SessionFactory sessionFactory;
public HibernateConfig() {
Configuration config = new Configuration();
config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
//change the next line of code to match your MySQL URL and port
config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:7071/test");
//change the next two lines of code to match your MySQL user name and password.
config.setProperty("hibernate.connection.username", "bob");
config.setProperty("hibernate.connection.password", "somepass");
//change the pool size to reflect how many users you expect your application to have initially
config.setProperty("hibernate.connection.pool_size", "10");
config.setProperty("hibernate.connection.autocommit", "true");
config.setProperty("hibernate.id.new_generator_mappings", "false");
config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
/*
* un-comment the next line of code if you want to be able to drop and recreate tables for your data classes listed below. This is generally a bad idea for security reasons.
*/
//config.setProperty("hibernate.hbm2ddl.auto", "create-drop");
config.setProperty("hibernate.show_sql", "true");
config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
config.setProperty("hibernate.current_session_context_class", "thread");
/*
* Add your classes here that you want to match your database tables
* The example has a User and a PhoneNumber class.
*/
config.addAnnotatedClass(User.class);
config.addAnnotatedClass(PhoneNumber.class);
/*
* There have been several changes to the Hibernate libraries.
* Please uncomment the source code for the version of Hibernate you are using.
*/
/*Hibernate 4.3 - 5.x */ //ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
/*Hibernate 3.x - 4.2*/ ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}