复制 HibernateSessionFactory.java 到 util 包下
package util; import org.hibernate.HibernateException; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; // 给线程绑定一个Session 比类级别更加安全 private static final ThreadLocal threadLocal = new ThreadLocal(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; // 运行一次 先于类构造方法完成 内存寄存器位置 static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } // 获得连接 public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } // 关闭资源 public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null; } public static Configuration getConfiguration() { return configuration; } }
实例化该类
Session session = HibernateSessionFactory.getSession();
释放资源操作
HibernateSessionFactory.closeSession();
PS:实例化 HibernateSessionFactory 类,就可以使用了,使用完后,就需要释放资源,关闭Session。
例子:
package test; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import util.HibernateSessionFactory; import entity.Student; import entity.Teacher; public class MaryToOneTest2 { public static void main(String[] args) { maryToOne(); } private static void maryToOne() { Session session = HibernateSessionFactory.getSession(); // 增删改用事务 Transaction tx = null; Teacher teacher =new Teacher("李老师"); Student student1 = new Student("A学生"); Student student2 = new Student("B学生"); try { tx=session.beginTransaction(); session.save(teacher); session.save(student1); session.save(student2); tx.commit(); System.out.println("保存成功!!!"); } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } } }
效果图:
相关推荐
HibernateSessionFactory.java
Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类
Hibernate中涉及到的增加,删除,查询,修改,操作.
hibernate自动生成关键映射以及工厂类,连接数据库弄好连接和设置hibernate...直接使用HibernateSessionFactory 工厂类调用方法测试就好了,方便简洁,还能提高编码效率。 本人亲自测试过,纯手打的。希望大家支持。
默认的hibernate配置文件名为hibernate.cfg.xml,当然这个文件名称和目录是可以修改的,但是,你必须在HibernateSessionFactory类中将CONFIG_FILE_LOCATION属性设置为实际的配置文件的名称和目录 该配置文件中描述...
此处笔者提供了关于hibernate的实用工具,包括hibernate.cfg.xml和HibernateSessionFactory.java类
hibernate开发的工具类,封装的sessionFactory,session等
生成HibernateSessionFactory工具类(HibernateUtil) ===利用Myeclipse根据表生成实体类和映射文件=== g.进入DB Browser窗口,找到数据表 h.数据表右键,选择Hibernate reverse Engin... i.选择要生成的文件hbm....
(5)创建Hibernate的SessionFactory类。 (6)通过SessionFactory创建Session实例。 (7)通过创建的Session实例进行持久化对象的管理。 (8)通过创建的Transaction实例进行事务管理。 (9)通过创建的Query或...
Hibernate5.2.1 的工具类 创建session 和 sessionFactory
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
ConfigurationConfiguration类类 SessionFactorySessionFactory接口接口 ...熟练使用熟练使用HibernateHibernate的各个接口的各个接口 使用使用HiberntaeHiberntae的的HQLHQL语言进行数据库访问语言进行数据库访问
统一管理SessionFactory,分散注册实体类。平台依赖度低的解决方案 详细介绍见http://blog.csdn.net/flyxxxxx 测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、...
} } <br> 上面的UserDAO实现了自定义的IUserDAO接口,并扩展了抽象类: HibernateDaoSupport HibernateSupport实现了HibernateTemplate和SessionFactory实例的关联。 HibernateTemplate对...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
2、 观察持久化类与数据库表的映射关系,观察相应的 Hibernate 映射文件 (.hbm.xml)配置,并能够做简单应用; 3、 观察 Hibernate 配置文件(hibernate.cfg.xml)中的主要元素及属性配置,并能够做简单应用。
hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...
初始化Hibernate:在要使用Hibernate的类的方法中实例化Configuration对象并用Configuration对象的configure()方法将hibernate.cfg.xml中的配置加载到内存,即: Configuration config = new Configuration()....