hibernate基本概念

HiberNate基本概念

阻抗不匹配:java语言是面向对象的,其中有继承等面向对象的特征。可是我们的关系数据库无法保存这种面向对象的特征,比如继承。这就是阻抗不匹配。这也是Habernate出现的原因。

透明持久化:当我们保存一个类的对象,比如说user,我们这个类不需要实现 Habernate的任何类或者接口。这个user对象是个最纯粹的对象,我们称这种对象为pojo。也就是没有侵入性(没有侵入Habernate的任何内容),这种没有侵入性的框架称为轻量级框架。

EJB之所以被成为重量级框架就是因为它的实现有侵入性。

没有侵入性的好处:可以随意更换框架。非常灵活。

Pojo:一个没有实现任何第三方的类或者接口的对象。

第一个Hibernate项目的配置

我的第一个hibernate项目,注意这里的步骤

1.创建java工程

2.创建user library创建流程:(window-preference-java-User Libraries)

需要在这里加入如下jar

1.Hibernate_Home/hibernate3.jar

2.Hibernate_Home/lib/.jar

3.mysql jdbc驱动



3.从hibernateHome\etc下复制hibernate.cfg.xml和log4j.properties文件到java项目的src根目录下

对hibernate.cfg.xml进行相关信息的配置,删除里面原始的信息,配置好以后是这个样子

<hibernate-configuration>

<session-factory>

配置数据库连接

<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>

数据库驱动

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

数据库名

<property name="hibernate.connection.username">root</property>

密码

<property name="hibernate.connection.password">itcast</property>

配置数据库适配器,这使用myslq方言。方便以后更改数据库

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

</session-factory>

</hibernate-configuration>

上面的配置的name和后面的值都来自

\hibernate-3.2\etc\hibernate.properties

在这个文件中有所有需要配置的属性的name和value。罗列mysql的部分如下(上下相同颜色的一一对应)

## MySQL

#hibernate.dialect org.hibernate.dialect.MySQLDialect

#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect

#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect

#hibernate.connection.driver_class com.mysql.jdbc.Driver

#hibernate.connection.url jdbc:mysql:///test

#hibernate.connection.username gavin

#hibernate.connection.password

加入log4j.properties是为了便于调试



4.创建实体类



5.定义User类的映射文件User.hbm.xml(在hibernateHome\eg\org\hibernate\auction下有这个文件的例子,拷贝过来修改下就可以用了),这个映射文件和实体类放在一起

把里面的内容全部删除,只保留如下形式

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

</hibernate-mapping>





6.把映射文件User.hbm.xml加入到hibernate.cfg.xml中

加好以后是这个样子

<mapping resource="com/itcast/hibernate/User.hbm.xml"/>

7.编写hbm2ddl工具类,把实体类生成数据库表

//读取hibernate.cfg.xml文件

Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

//把实体类生成数据库表

export.create(true, true);

8、开发客户端

为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入

<property name="hibernate.show_sql">true</property>

这样就现实sql语句

保存对象到数据库的方法代码

//读取hibernate.cfg.xml文件

Configuration cfg = new Configuration().configure();

//创建SessionFactory,这个东西是一个数据库对应一个

SessionFactory factory = cfg.buildSessionFactory();

Session session = null;

try {

session = factory.openSession();

//session默认的autocommit=false。所以这里必须自己手动开启和提交事务。

//开启事务

session.beginTransaction();

User user = new User();

user.setName("张三");

user.setPassword("123");

user.setCreateTime(new Date());

user.setExpireTime(new Date());

//保存数据,这个时候如果id的生成策略不需要数据库参与(如uuid),这个时候会把相关数据放入session的缓存,不会发出sql

session.save(user);

//提交事务,提交事务前要获得这个事务的上下文:“getTransaction()”才能提交

session.getTransaction().commit();

}catch(Exception e) {

e.printStackTrace();

//回滚事务

session.getTransaction().rollback();

}finally {

if (session != null) {

if (session.isOpen()) {

//关闭session

session.close();

}

}

}

注意上面的代码//读取hibernate.cfg.xml文件

Configuration cfg = new Configuration().configure();

这个Configuration 对象是专门用于加载配置文件的,但是如果我们只new Configuration()的话,它默认只加载.property文件,而不是我们现在用的xml文件,所以必须使用new Configuration().configure(); 才可以正确加载我们的xml文件。

SessionFactory是和数据库绑定的,一个数据库对应一个SessionFactory。非常耗时,最好只创建一次。它是线程安全的。

Session :并不代表connection,它对connection进行了一层的包装。open一个Session并不代表就open了一个connection。只有当这个connection真正需要使用的时候才会去连接池中去拿。

Session还对实体对象的生命周期进行管理。

Session线程不安全,通常一个session对应一个事务,事务关闭session关闭

SessionFactory

应用程序从SessionFactory里获得Session(会话)实例,这个 SessionFactory可以在多个线程共享,通常SessionFactory在整个应用中只创建一次,但如果你需要访问多个数据库,就需要为每个数据库创建一个SessionFactory。SessionFactory中缓存了生成的sql和hibernate运行是使用的映射元数据。

说明:SessionFactory由Configuration对象创建,所以每个hibernate配置文件实际上是对SessionFactory的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值