Java如何创建Hibernate的SessionFactory?
在创建应用程序时,使用Hibernate管理我们的应用程序持久性对象,我们将需要一个SessionFactory。该工厂创建或打开一个会话以与数据库对话。
要创建一个,SessionFactory我们可以在中定义配置hibernate.properties,hibernate.cfg.xml或以编程方式创建它。在此示例中,我们将使用hibernate.cfg.xml配置文件,该文件通常在创建Hibernate应用程序时使用。
以下是我们的会话配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/musicdb</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<!-- JDBC connection pool, use Hibernate internal connection pool -->
<property name="connection.pool_size">5</property>
<!-- Defines the SQL dialect used in Hibernate's application -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
<!-- Display and format all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping to hibernate mapping files -->
<mapping resource="org/nhooo/example/hibernate/model/Label.hbm.xml"/>
</session-factory>
</hibernate-configuration>现在,我们已经完成了配置文件,让我们创建一个用于配置和构建SessionFactory对象的帮助器类。该帮助器将在此站点的其他Hibernate示例中使用。
package org.nhooo.example.hibernate.app;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionFactoryHelper {
private static final SessionFactory sessionFactory;
static {
try {
//从会话工厂配置构建SessionFactory对象
//在hibernate.cfg.xml文件中定义。在这个文件中,我们
//注册JDBC连接信息,连接池,
//我们使用的休眠方言及其映射
//每个pojo的hbm.xml文件(纯旧的Java对象)。
Configuration config = new Configuration();
sessionFactory = config.configure().buildSessionFactory();
} catch (Throwable e) {
System.err.println("Error in creating SessionFactory object."
+ e.getMessage());
throw new ExceptionInInitializerError(e);
}
}
public static void main(String[] args) {
Session session = SessionFactoryHelper.getSessionFactory()
.getCurrentSession();
System.out.println("session = " + session);
}
/**
* A static method for other application to get SessionFactory object
* initialized in this helper class.
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}Maven依赖
<dependencies>
<!--https://search.maven.org/remotecontent?filepath=org/hibernate/hibernate-core/5.4.1.Final/hibernate-core-5.4.1.Final.jar-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
<!--https://search.maven.org/remotecontent?filepath=org/hibernate/hibernate-ehcache/5.4.1.Final/hibernate-ehcache-5.4.1.Final.jar-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.1.Final</version>
</dependency>
<!--https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>