package com.demo.Util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
/**
* 单例模式
* @author Administrator
*
*/
public class HibernateUtil
{
//1.声明需要初始化的private单例属性(静态常量static final)
private static final SessionFactory sessionFactory = buildSessionFactory();
//2.声明private的初始化单例的方法
private static SessionFactory buildSessionFactory() {
// A SessionFactory is set up once for an application!
// final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
// .configure() // configures settings from hibernate.cfg.xml
// .build();
StandardServiceRegistry registry = null;
try {
registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
return new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}catch (Exception e) {
e.printStackTrace();
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
return null;
}
//3.提供公共外部获取单例方法
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
新版本hibernate工厂类代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!--
create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
-->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping -->
<!-- <mapping resource="com/demo/ex01/entity/User.hbm.xml"/> -->
<!-- 简单属性映射
<mapping class = "com.demo.ex01.entity.User" />
<mapping class = "com.demo.ex02.entity.User" />
-->
<!-- 继承关系映射 -->
<!--
<mapping class = "com.demo.ex03.entity.Plane"/>
<mapping class = "com.demo.ex03.entity.A320"/>
<mapping class = "com.demo.ex04.many2one.entity.Flight"/>
<mapping class = "com.demo.ex04.many2one.entity.Company"/>
<mapping class = "com.demo.ex04.one2many.entity.Orders"/>
<mapping class = "com.demo.ex04.one2many.entity.OrderItems"/>
<mapping class = "com.demo.ex04.one2one.entity.Body"/>
<mapping class = "com.demo.ex04.one2one.entity.Heart"/>
-->
<mapping class = "com.demo.ex04.mant2many.entity.House"/>
<mapping class = "com.demo.ex04.mant2many.entity.Person"/>
<mapping class = "com.demo.ex04.self2self.entity.Menu"/>
</session-factory>
</hibernate-configuration>
hibernate-xml配置文件