-
Notifications
You must be signed in to change notification settings - Fork 0
/
HibernateUtil1.java
50 lines (38 loc) · 1.47 KB
/
HibernateUtil1.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
package utility;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import model.ViecCanLam;
public final class HibernateUtil {
private static final SessionFactory FACTORY;
static {
Configuration conf = new Configuration();
Properties properties = new Properties();
properties.put(Environment.DIALECT, "org.hibernate.dialect.SQLServerDialect");
properties.put(Environment.DRIVER, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
properties.put(Environment.URL, DbMetadata.getConnectString());
properties.put(Environment.SHOW_SQL, "true"); // Hiển thị câu lệnh SQL thực hiện
properties.put(Environment.FORMAT_SQL, "true");
// properties.put(Environment.HBM2DDL_AUTO, "create"); // tự động sinh db
conf.setProperties(properties);
// conf.addAnnotatedClass(ViecCanLam.class);
ServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties())
.build();
FACTORY = conf.buildSessionFactory(registry);
}
public static SessionFactory getFactory() {
return FACTORY;
}
public static synchronized Session getSession() {
return FACTORY.openSession();
}
public static void main(String[] args) {
getFactory();
System.out.println("Hibernate run successfully!");
}
}