Hibernate 核心的API
来源:互联网 发布:淘宝海底捞优惠券 编辑:程序博客网 时间:2024/05/09 17:06
Configuration cfgConfiguration=new Configuration();SessionFactory s = cfgConfiguration.configure().buildSessionFactory();Session ss=s.openSession();ss.beginTransaction();ss.save(ip_List);ss.save(student);ss.getTransaction().commit();ss.close();s.close();
1.Configuration
用于获取hibernate的配置文件 默认文件名为hibernate.cfg.xml;也可以自己配置路径
通过configure()方法获取配置信息;
2.SessionFactory
session工厂类 用于获取Session对象 有两种获取方法:
a. openSession() 用于建立一个新的Session会话 需要Close
b. getCurrentSession 用于从上下文中获取一个会话 不存在 则建立新的会话(推荐) 上下文通常配置在Hibernate配置文件中,thread为从当前线程获取。
3.Session
一次数据库连接会话;数据库的sql操作都执行于Session会话中;不过所有处理都必须按照事务处理
a.session.beginTransaction() 开始事务
b.session.getTransaction().commit();提交事务
c.save(Object o) 插入操作
b.delete(Object o) 删除操作;所传的实体 必须有ID 否则会出异常
e.load(theClass, id) 查询操作 延迟化查询 再关闭事务之前调用查询到的对象时 才执行select操作/ 若事务关闭了 再调用实体 会有异常
get(theClass, id) 查询操作 立刻查询
f. update(Object o) 更新操作
利用HQL 执行查询 注意操作的是具体的实体类 而不是表;org.hibernate.Query query=session.createQuery(hql); //利用query接口实现查询
详见:http://blog.csdn.net/zhuali_linkin/article/details/7273276
g. clear 清除缓存(session)中的所有持久化对象
h. flush 对缓存(session)中的对象做一次数据库提交
对象的三种状态
a 透明态 持久态 托管态
hibernate 常用的一个工厂类
package com.zhuxuli.HibernateSession;import javax.persistence.Entity;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;/** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */@Entitypublic class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); 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() { }/** * Returns the ThreadLocal Session instance. Lazy initialize * the <code>SessionFactory</code> if needed. * * @return Session * @throws HibernateException */ 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; }/** * Rebuild hibernate session factory * */public static void rebuildSessionFactory() {try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {System.err.println("%%%% Error Creating SessionFactory %%%%");e.printStackTrace();}}/** * Close the single hibernate session instance. * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } }/** * return session factory * */public static org.hibernate.SessionFactory getSessionFactory() {return sessionFactory;}/** * return session factory * *session factory will be rebuilded in the next call */public static void setConfigFile(String configFile) {HibernateSessionFactory.configFile = configFile;sessionFactory = null;}/** * return hibernate configuration * */public static Configuration getConfiguration() {return configuration;}}
- Hibernate 核心的API
- Hibernate的核心API
- Hibernate的五个核心API
- 5.Hibernate核心的API
- Hibernate核心API的详解
- Hibernate核心API(理解)
- Hibernate核心API
- Hibernate:核心API
- Hibernate核心API
- Hibernate核心API
- hibernate核心API简介
- hibernate核心api
- Hibernate入门----核心API
- hibernate核心API介绍
- Hibernate核心API详解
- Hibernate 核心 api
- hibernate核心API
- Hibernate核心API
- 产品设计界面截图
- mexFunction 解读
- 虚拟机安装 错误
- GNU PRIVACY HANDBOOK
- Ajax 与 Comet
- Hibernate 核心的API
- Android中的Button控件
- JAVA基础加强之注解
- 老公吃软饭还和小三合伙骗我钱
- ACM里的一些缩写
- windows下安装Latex的安装
- OEM12C EMCLI安装
- persist、merge、save总结
- IT精英,必须掌握的命令行