BluewineY 2016-09-23 05:53 采纳率: 50%
浏览 2186
已采纳

我的session.isOpen()报错,Hibernate的工厂类不会写

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(); 
            } 
        } 



}

  • 写回答

2条回答 默认 最新

  • AProMonkey 2016-09-23 06:44
    关注

    下面是我写的一个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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?