hibernate3.x版本

 package hibernate_01;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class hibernateUtils {
private static Configuration cfg = null;
private static SessionFactory sessionFactory = null;
private static Session session = null;
static{
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}
public static Session getSession(){
if(sessionFactory!=null){
session = sessionFactory.openSession();
}else{
sessionFactory = cfg.buildSessionFactory();
}
return session = sessionFactory.openSession();
}
public static void closeSession(){
if(session != null && session.isOpen()){
session.close();
}
}
}

hibernate4.x

 package cn.siggy.util;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
private static Configuration cfg=null;
private static SessionFactory factory=null;
private static Session session = null;
static{
cfg = new Configuration().configure();
factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
}
public static Session getSession(){
if(factory!=null)
return factory.openSession();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return session = factory.openSession();
}
public static void closeSession(){
if(session!=null&&session.isOpen())
session.close();
}
}

hibernateUtils的更多相关文章

  1. 使用 Hibernate 完成 HibernateUtils 类 (适用于单独使用Hibernate或Struts+Hibernate)

    package com.istc.Utilities; import org.hibernate.Session; import org.hibernate.SessionFactory; impor ...

  2. Hibernate 提供session的工具类HibernateUtils

    package cn.itcast.utils; import java.sql.Connection; import java.sql.SQLException; import org.hibern ...

  3. Hibernate 命名查询

    1.定义 Hibernate允许在映射配置文件中定义字符串形式的查询语句,这种查询方式被称为命名查询. 2.创建表结构并添加测试数据 create table `student` ( `id` dou ...

  4. Java三大框架之——Hibernate中的三种数据持久状态和缓存机制

    Hibernate中的三种状态   瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...

  5. Hibernate框架之Criteria查询 和注解(重点☆☆☆☆☆,难点☆☆☆)

    写好一篇博客,不是容易的事.原因是:你要给自己以后看的时候,还能看懂,最重要的是当别人看到你的博客文章的时候,也一样很清楚的明白你自己写的东西.其实这也是一种成就感!! 对于每一个知识点,要有必要的解 ...

  6. Hibernnate延迟加载策略(这么详细你还看不懂)

    好久没有认真写过博客了,今天就好好的写一篇吧!!!!!!!!! 当Hibernate 从数据库中加载某个对象(例如:Dept对象)时,如果同时自动加载所有的关联的某个对象(例如:Emp对象),而程序实 ...

  7. 【Hibernate框架】对象的三种持久化状态

    一.综述 hibernate中的对象有三种状态,分别是TransientObjects(瞬时对象).PersistentObjects(持久化对象)和DetachedObjects(托管对象也叫做离线 ...

  8. JPA或Hibernate中的

    JPA执行原生SQL截断Char类型问题 在JPA的API中执行原生SQL:EntityManager.createNativeQuery(String sqlString); 传入参数是原生SQL语 ...

  9. hibernate理解

    SSH框架: Struts框架, 基于mvc模式的应用层框架技术! Hibernate,基于持久层的框架(数据访问层使用)! Spring,创建对象处理对象的依赖关系以及框架整合! Dao代码,如何编 ...

随机推荐

  1. c 函数及指针学习 5

    聚合数据类型 能够同时存储超过一个的单独数据. c语言提供了数组和结构体. 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> # ...

  2. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  3. kuangbin_UnionFind C (HDU 1213)

    过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...

  4. kmeans算法

    # coding:utf-8 import numpy as np import matplotlib.pyplot as plt def dis(x, y): #计算距离 return np.sum ...

  5. C#联调C++项目

    很多人在编写C#代码时,经常要调用C++代码,有时我们通常用打日志来查看运行状况,这当然可以,不过这样挺不方便,一遍遍的跑代码,一遍遍的看日志,感觉如果可以直接把断点打入C++的代码就好了,其实是可以 ...

  6. python--类方法、对象方法、静态方法

    1.我们已经讨论了类/对象可以拥有像函数一样的方法,这些对象方法与函数的区别只是一个额外的self变量 # -*- coding:utf-8 -*- #!/usr/bin/python # Filen ...

  7. 8000401a 错误 ,检索 COM 类工厂中 CLSID 为 的组件时失败,原因是出现以下错误: 8000401a。

    "/"应用程序中的服务器错误. -------------------------------------------------------------------------- ...

  8. 一个LINUX狂人的语录(个人认为很精辟)

    http://blog.chinaunix.net/uid-57160-id-2734431.html?page=2 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的 ...

  9. 虚拟化之vmware-vcenter

    VMware ESXi.VMware vCenter Server 和 vSphere Client,它们分别是 vSphere 的虚拟化层.管理层和接口层. vcenter server 插件和vc ...

  10. hydra

    转:http://www.cnblogs.com/patf/p/3142564.html 1.yum -y install openssl-devel pcre-devel ncpfs-devel p ...