Hebernate 的SessionFactory怎么写,我的session.isOpen()报错,让buildPath
我的SessionFactory这样写的,写了两天了,也没写好,也不能完成基本的增删改查
package com.aisino.hibernate.source;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HebernateUtil {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
//SessionFactory的对象
private static SessionFactory sessionFactory=null;
//静态块
static{
//加载Hibernate配置文件
Configuration cfg=new Configuration().configure();
//sessionFactory=cfg.buildSessionFactory(new ServiceRegistry());
}
//获取Session
public static Session getSession() throws HibernateException{
Session session=threadLocal.get();
if(session==null || !session.isOpen()){
rebuildSessionFactory();
}
session=(sessionFactory !=null)?sessionFactory.openSession():null;
threadLocal.set(session);
}
//重建会话工厂
private static void rebuildSessionFactory() {
// TODO Auto-generated method stub
}
//关闭Session
public static void closeSession() throws HibernateException {
Session session = threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
}
下面是我写的一个Hibernate工具类,用于生成session
package com.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @Author: zsh
* @Title: HibernateTool.java
* @Description:建立一个工具类,主要是得到session及关闭session
* @Date:2016年9月23日 上午14:25:22
*/
public class HibernateTool {
private static Configuration cfg = null;
private static SessionFactory factory = null;
private static Session session = null;
// 静态代码块:优先于主方法,且只执行一次
static {
try {
cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
/**
* 获得Session对象
*
* @return
*/
public static Session getSession() {
session = factory.openSession();
return session;
}
/**
* 关闭session
*
* @param session
*/
public static void closeSession(Session session) {
try {
if (session.isOpen())
session.close();
} catch (HibernateException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
下面是hibernate的配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- session工厂,负责管理session -->
<session-factory name="foo">
<!-- 配置Oracle属性文件 -->
<!-- Hibernate方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 配置驱动 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 配置URL -->
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 配置用户名 -->
<property name="hibernate.connection.username">scott</property>
<!-- 配置密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 是否显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 设置是否产生关系表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 格式化SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 如果数据库中没有该表,则自动创建表 -->
<!-- hbm2ddl.auto有几种选项,update是当类的字段有更新时,运行程序后会自动更新对应的表 -->
<!--
<property name="hbm2ddl.auto">create</property>
-->
<!-- 配置映射文件 -->
<!-- Users表的映射,测试数据库操作时需要使用 -->
<mapping resource="com/hibernate/bean/Users.hbm.xml"/>
<!-- 测试表的关系映射时需要使用 -->
<mapping resource="com/hibernate/bean/DistrictDemo1.hbm.xml"/>
<mapping resource="com/hibernate/bean/StreetDemo1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建工厂类
package cn.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
步4:测试是否连接数据库成功 – 获取 Connection对象
@Test
public void test1() {
// 1:获取 SessionFactory
SessionFactory sf = SessionFactoryUtils.getSessionFatory();
// 打开一个新的连接会话
Session session = sf.openSession();//
// 通过doWork获取一个COnnection,则所有在execute里面执行的方法都被Session控制
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.err.println("连接是:" + connection);
}
});
session.close();
}
package cn.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
创建一个工厂类用于创建SessionFactory唯一的一个
*/
publicclass SessionFactoryUtils {
privatestatic SessionFactory sessionFactory;
// 在静态的代码块中创建这个对象
static {
// 1:创建Configuration对象,用于读取hibernate.cfg.xml文件
Configuration config = new Configuration();
// 默认读取hibernte.cfg.xml
config.configure();
// 2:创建SessionFactory对象
sessionFactory = config.buildSessionFactory();
}
//3:提供一个静态的方法-返回SessionFactory的实例
publicstatic SessionFactory getSessionFatory(){
returnsessionFactory;
}
}
步4:测试是否连接数据库成功 – 获取 Connection对象
@Test
publicvoid test1() {
// 1:获取 SessionFactory
SessionFactory sf = SessionFactoryUtils.getSessionFatory();
// 打开一个新的连接会话
Session session = sf.openSession();//
// 通过doWork获取一个COnnection,则所有在execute里面执行的方法都被Session控制
session.doWork(new Work() {
@Override
publicvoid execute(Connection connection) throws SQLException {
System.err.println("连接是:" + connection);
}
});
session.close();
}