创建Hibernate配置文件
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property
name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property
name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
</property>
<property
name="connection.username">jbit</property>
<property
name="connection.password">bdqn</property>
<property
name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property
name="show_sql">true</property>
<!--省略其他配置-->
<!--注意配置文件名必须包含其相对于classpath
的全路径-->
<mapping
resource="cn/jbit/houserent/entity/User.hbm.xml"
/>
</session-factory>
</hibernate-configuration>
配置User类的映射文件
<hibernate-mapping>
<class
name="cn.jbit.houserent.bean.User"
table="users">
<id
name="id"
type="java.lang.Integer">
<column
name="id"
/>
<generator
class="sequence"
>
<param
name="sequence">SEQ_ID</param>
</generator>
</id>
<property
name="name"
type="java.lang.String">
<column
name="name"
length="50"
not-null="true"
/>
</property>
<property
name="password"
type="java.lang.String">
<column
name="password"
length="50"
not-null="true"
/>
</property>
<!--省略其他配置-->
</class>
</hibernate-mapping>
使用Hibernate操作数据库需要七个步骤
hibernate-mapping>
1.读取并解析配置文件
Configuration conf = new Configuration().configure();
2.读取并解析映射信息,创建SessionFactory
SessionFactory sf = conf.buildSessionFactory();
3.打开Session
Session session = sf.openSession();
//sf.getCurrentSession();
4.开始一个事务(增删改操作必须,查询操作可选)
Transaction tx = session.beginTransaction();
5.数据库操作
session.save(user); //或其他操作
6.提交事务(回滚事务)
tx.commit(); (tx.rollback();)
7.关闭session
如果Hibernate配置文件中,current_session_context_class参数设置为thread并采用SessionFactory的getCurrentSession()方法获得Session实例则不需要此步
session.close();
代码:
Configuration
conf
= null;
SessionFactory
sessionFactory
=
null;
Session
session
=
null;
Transaction
tx
=
null;
try {
conf = new
Configuration().configure();
sessionFactory
=
conf.buildSessionFactory();
session = sessionFactory.openSession();
tx
=
session.beginTransaction();
session.save(user);
tx.commit();
}
catch (HibernateException
e)
{
tx.rollback();
e.printStackTrace();
} finally{
session.close();
sessionFactory.close();
}
使用Hibernate实现对数据库的查询操作:
Object
get(Class class,Serializable id)
Object
load(Class theClass,Serializable id)
两种方法都可以加载数据,但有一定区别
session.load(User.class,1002);
或者: session.get(User.class,1002);
当使用Session的get方法时,如果加载的数据不存在,get方法会返回一个null对象
使用load方法时,如果加载的数据不存在,系统就会抛出异常
get方法即时加载
load方法支持lazy延迟加载
Hibernate中实体对象的三种状态:
瞬时状态(Transient)
刚用new语句创建,还没有被持久化,且不处于Session的缓存中
持久状态(Persistent)
已经被持久化,且加入到Session的缓存中
游离状态(Detached)
已经被持久化,但不再处于Session的缓存中
总结:
Hibernate是一个基于JDBC的持久化解决方案,是一个优秀的"对象一关系映射"框架
使用Hibernate前要做好以下三个准备。
1.添加需要的jar包
2.配置Hibernate配置文件
3.创建实体类和映射文件
使用Hibernate完成持久化操作需要以下七个步骤
1.读取并解析配置文件
2.读取并解析映射文件,创建SessionFactory
3.打开Session
4.开启事务(查询操作不需要)
5.执行持久化方法
6.提交事务
7.关闭Session
使用提取重复代码到基类的技巧可以简化“七个步骤”
Hibernate中将所操作的实体对象的状态分为三种,分别为Transient(瞬时状态)
Persistent(持久状态) 以及Detached(游离状态)
注意:
openSession用一次
用getSession获得session省资源
加载Hibernate的jar包:
需要f:\hibernate-distribution-3.3.2.GA\Hibernate3.jar和f:\hibernate-distribution-3.3.2.GA\lib\required目录下的所有jar包
还需加载JDBC驱动包
事务写在业务层,若写在DAO层,无论执行何种操作都只是操作的一个对象,不存在事务的价值
session管理50000个操作,若超出范围则会异常,使用假提交,把压力扔给数据库
getSession.flush(); //Hibernate批量处理
getSession.clear();