本例以Windows, JDK1.8, MySQL5 为基础,来演示对Hibernate5中对SessionFactory的获取。

Hibernate V5.x 相对于V 4.x 在获取 SessionFactory 上面还是存到着一些不同之处。下面来演示怎样获取SessionFactory:

通过Properties来配置信息

将属性配置以Properties的方式来设置,可以通过Properties文件,也可以直接使用Properties对象。

HibernateProUtil.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
package com.devnp.hibernate.core.util;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import com.devnp.hibernate.core.model.Student;

public class HibernateProUtil {

private static SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
Properties properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect"); //数据库方言
properties.put("hibernate.show_sql", "true"); //是否打印SQL
properties.put("hibernate.hbm2ddl.auto","update"); //执行跟新,及如果表不存在则会自动创建表

properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.put("hibernate.connection.password", "!qaz2wsx");
properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/test");
properties.put("hibernate.connection.username", "root");

/*StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySettings(properties);

MetadataSources metadataSources = new MetadataSources(standardServiceRegistryBuilder.build());
metadataSources.addAnnotatedClass(Student.class);*/

StandardServiceRegistry serviceRegistry= new StandardServiceRegistryBuilder().applySettings(properties).build();

MetadataSources metadataSources = new MetadataSources(serviceRegistry);
metadataSources.addAnnotatedClass(Student.class);

Metadata metadata = metadataSources.getMetadataBuilder().applyImplicitNamingStrategy(
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE).build();

sessionFactory = metadata.getSessionFactoryBuilder().build();

return sessionFactory;
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}

通过XML来配置信息

将所有配置信息配置到hibernate.cfg.xml文件当中

hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">!qaz2wsx</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.devnp.hibernate.core.model.Student"/>
</session-factory>
</hibernate-configuration>

HibernateXmlUtil.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

package com.devnp.hibernate.core.util;

import java.util.EnumSet;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

public class HibernateXmlUtil {

private static SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {

StandardServiceRegistry serviceRegistry= new StandardServiceRegistryBuilder().configure().build();

Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE).build();

/*MetadataSources metadataSources = new MetadataSources(serviceRegistry);

SchemaExport schemaExport = new SchemaExport();

schemaExport.create(EnumSet.of(TargetType.DATABASE), metadataSources.buildMetadata());

sessionFactory = metadataSources.buildMetadata().buildSessionFactory();*/

sessionFactory = metadata.getSessionFactoryBuilder().build();

return sessionFactory;
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}

关于更多属性配置可以参考官方文档:

https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html