hibernate的session

本文详细介绍了Hibernate的Session接口,它是数据库操作的关键。SessionFactory通常只初始化一次,而Session代表一次数据库操作,非线程安全。讨论了通过SessionFactory获取Session的两种方式,并阐述了Session对数据库的增删改查操作。此外,还详细解析了Hibernate中的三种对象状态:瞬态、持久态和托管态,以及它们之间的转换方法。
摘要由CSDN通过智能技术生成

hibernate的session是持久化管理器接口,我们用它来从数据库中存取数据。得到Session之前首先要得到sessionFactory。SessionFactory负责一个数据库,也只对应一个XML配置文件(hibernate.cfg.xml),SessionFactory通常只被初始化一次。SessionFactory是线程安全的,很多线程可以同时访问它,获取Session。Session不是线程安全的,它代表与数据库之间的一次操作。Session通过SessionFactory打开,在所有的工作完成后,需要关闭
Session的得到
方式一:
当在Myeclipse8以上的版本引入Myeclipse自带的Hibernate框架后,myeclipse会自动为项目创建一个HibernateSessionFactory类。
代码如下

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 }.
 */
@Entity
public 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值