一、是什么
Hibernate是开源O/R映射框架,我们平时使用JDBC操作数据库,步骤繁琐,而且操作是关系型数据库,但在java程序开发使用面向对象思想,hibernate正式在这两种不同模型之间建立关联,提供利用面向对象思想来操作关系型数据的接口,可以说是对JDBC进一步的封装。
二、基本核心接口
JNDI 、JDBC、JTA
三、持久化对象三种状态
瞬时对象(Transient Objects):使用new操作符初始化的对象不是立刻就持久的。它们的状态是瞬时的,也就是说它们没有任何跟数据库表相关联的行为,只要应用不再引用这些对象(不再被任何其它对象所引用),它们的状态将会丢失,并由垃圾回收机制回收。
(没有被session管理,在数据库中没有与之匹配的记录)
持久化对象(Persist Objects):持久实例是任何具有数据库标识的实例。它有持久化管理器Session统一管理,持久实例是在事务中进行操作的——它们的状态在事务结束时同数据库进行同步。当事务提交时,通过执行SQL的INSERT、UPDATE和DELETE语句把内存中的状态同步到数据库中。
(纳入session管理,在数据库中有与之匹配的记录,当属性发生改变,在清理缓存时(脏数据检查)会自动和数据库同步 )
离线对象(Detached Objects):Session关闭之后,持久化对象就变为离线对象。离线表示这个对象不能再与数据库保持同步,它们不再受Hibernate管理。
(没有被session管理, 在数据库中存在与之匹配的记录)
四、开发步骤
1、引入Hibernate及其依赖库(jar包)包括引入mysql数据库驱动包
2、打开mysql控制台,创建测试数据库”hibernate”
Create database hibernate;
Use hibernate;
hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!--
<property name="hibernate.format_sql">true</property>
-->
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
补充:Hibernate配置两种方法
属性文件(hibernate.properties)
调用代码:Configuration cfg= new Configuration();
Xml文件(hibernate.cfg.xml)
调用代码:Configuration cfg= new Configuration().configure();
持久化类User
package com.bjpowernode.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
类的映射文件User.hbm.xml
<?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>
<class name="com.bjpowernode.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
创建数据库表工具类ExportToDB:
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author Administrator
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
//创建数据库表
export.create(true, true);
}
}