Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate 可以应用在任何使用 JDBC 的场合,既可以在 Java 的客户端程序使用,也可以在 Servlet/JSP 的 Web 应用中使用,最具革命意义的是,Hibernate 可以在应用 EJB 的 J2EE 架构中取代CMP,完成数据持久化的重任。

官网地址为:http://hibernate.org/

Hibernate 的运用也不难,主要是一些配置,在这里分享一个视频,一个对照视频实练的小程序,还有一些 jar 包之类的,供大家学习工作用。

Hibernate 主配置文件 ​hibernate.cfg.xml​:


<?xml version='1.0' encoding='utf-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">psw123456</property>

<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->

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

<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->

<mapping resource="com/sinaapp/langtuteng/demo/hibernate/model/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>


对象的映射文件,和对象类文件放在一起,如 Student.hbm.xml:

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.sinaapp.langtuteng.demo.hibernate.model">
<class name="Student">
<id name="id"></id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>

当然需要在 MySQL 数据库中创建 hibernate 数据库,然后再数据库中建立 student 表。

在测试类中,如果用下面的方法取得 session 对象,则会提示方法 buildSessionFactory() 已废弃。

Configuration cfg = new Configuration();  
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
// 开启事务
session.beginTransaction();
// 保存对象
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.save(s);
// 事务提交
session.getTransaction().commit();
// 关闭资源
session.close();
sf.close();

所以用改用下面的方式获取:

Configuration cfg = new Configuration();  
cfg.configure("hibernate.cfg.xml");
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();
// 开启事务
session.beginTransaction();
// 保存对象
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.save(s);
// 事务提交
session.getTransaction().commit();
// 关闭资源
session.close();
sf.close();

另外,用 Annotation 来实现实体类的话,代码会更加简洁。比如再创建一个 Teacher 的实体类,用注解来标记类,如用 @Entity 来注解实体类,用 @Id 来注解表中的主键:


package com.sinaapp.langtuteng.demo.hibernate.model;  

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {
private int id;
private String name;
private String title;

@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}

需要在主配置文件中加入下面配置,来映射该实体类:

<mapping class="com.sinaapp.langtuteng.demo.hibernate.model.Teacher"/>

否则会报如下错误:




Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.sinaapp.langtuteng.demo.hibernate.model.Teacher



在测试代码中,原来实例化 Configuration 的话需要调用 AnnotationConfiguration() 方法,但在后来的版本中,该方法已废弃,可以用 Configuration() 方法。详细见 http://www.mkyong.com/hibernate/hibernate-the-type-annotationconfiguration-is-deprecated/ 的解释。




In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“.




So , you can safely replace your “​AnnotationConfiguration​” with “​Configuration​” class.




其它部分如果想参阅,请下载 Demo 附件吧,下面提供一些下载链接:

代码小 Demo + hibernate包 + mysql驱动包下载:百度云共享下载