@yudesong
2018-02-17T06:51:57.000000Z
字数 9886
阅读 586
Hibernate
Hibernate 是由 Gavin King 于 2001 年创建的开放源代码的对象关系框架。它强大且高效的构建具有关系对象持久性和查询服务的 Java 应用程序。Hibernate 将Java类映射到数据表中从Java 数据类型中映射到 SQL 数据类型中,并把开发人员从 95%的公共数据持续性编程工作中解放出来。
下面是一个详细的 Hibernate 应用程序体系结构视图以及一些重要的类。

配置对象
配置对象是你在任何 Hibernate 应用程序中创造的第一个 Hibernate 对象,并且经常只在应用程序初始化期间创造。它代表了 Hibernate 所需一个配置或属性文件。配置对象提供了两种基础组件。
SessionFactory 对象
配置对象被用于创造一个 SessionFactory 对象,使用提供的配置文件为应用程序依次配置 Hibernate,并允许实例化一个会话对象。SessionFactory 是一个线程安全对象并由应用程序所有的线程所使用。
SessionFactory 是一个重量级对象所以通常它都是在应用程序启动时创造然后留存为以后使用。每个数据库需要一个 SessionFactory 对象使用一个单独的配置文件。所以如果你使用多种数据库那么你要创造多种 SessionFactory 对象。
Session 对象
一个会话被用于与数据库的物理连接。Session 对象是轻量级的,并被设计为每次实例化都需要与数据库的交互。持久对象通过 Session 对象保存和检索。
Session 对象不应该长时间保持开启状态因为它们通常情况下并非线程安全,并且它们应该按照所需创造和销毁。
Transaction 对象
一个事务代表了与数据库工作的一个单元并且大部分 RDBMS 支持事务功能。在 Hibernate 中事务由底层事务管理器和事务(来自 JDBC 或者 JTA)处理。
这是一个选择性对象,Hibernate 应用程序可能不选择使用这个接口,而是在自己应用程序代码中管理事务。
Query 对象
Query 对象使用 SQL 或者 Hibernate 查询语言(HQL)字符串在数据库中来检索数据并创造对象。一个查询的实例被用于连结查询参数,限制由查询返回的结果数量,并最终执行查询。
Criteria 对象
Criteria 对象被用于创造和执行面向规则查询的对象来检索对象。
Session 用于获取与数据库的物理连接。 Session 对象是轻量级的,并且设计为在每次需要与数据库进行交互时被实例化。持久态对象被保存,并通过 Session 对象检索找回。
该 Session 对象不应该长时间保持开放状态,因为它们通常不能保证线程安全,而应该根据需求被创建和销毁。Session 的主要功能是为映射实体类的实例提供创建,读取和删除操作。这些实例可能在给定时间点时存在于以下三种状态之一:
Session session = factory.openSession();Transaction tx = null;try {tx = session.beginTransaction();// do some work...tx.commit();}catch (Exception e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}
在 Hibernate 中,其对象或实例将会被存储在数据库表单中的 Java 类被称为持久化类。
public class Employee {private int id;private String firstName;private String lastName;private int salary;public Employee() {}public Employee(String fname, String lname, int salary) {this.firstName = fname;this.lastName = lname;this.salary = salary;}public int getId() {return id;}public void setId( int id ) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName( String first_name ) {this.firstName = first_name;}public String getLastName() {return lastName;}public void setLastName( String last_name ) {this.lastName = last_name;}public int getSalary() {return salary;}public void setSalary( int salary ) {this.salary = salary;}}
一个对象/关系型映射一般定义在 XML 文件中。映射文件指示 Hibernate 如何将已经定义的类或类组与数据库中的表对应起来。
Employee.hbm.xml
create table EMPLOYEE (id INT NOT NULL auto_increment,first_name VARCHAR(20) default NULL,last_name VARCHAR(20) default NULL,salary INT default NULL,PRIMARY KEY (id));<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="Employee" table="EMPLOYEE"><meta attribute="class-description">This class contains the employee detail.</meta><id name="id" type="int" column="id"><generator class="native"/></id><property name="firstName" column="first_name" type="string"/><property name="lastName" column="last_name" type="string"/><property name="salary" column="salary" type="int"/></class></hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?><!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="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="connection.username">root</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="show_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="com/hibernate/User.hbm.xml"/></session-factory></hibernate-configuration>
操作数据库
public class ManageEmployee {private static SessionFactory factory;public static void main(String[] args) {try{factory = new Configuration().configure().buildSessionFactory();}catch (Throwable ex) {System.err.println("Failed to create sessionFactory object." + ex);throw new ExceptionInInitializerError(ex);}ManageEmployee ME = new ManageEmployee();/* Add few employee records in database */Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);Integer empID3 = ME.addEmployee("John", "Paul", 10000);/* List down all the employees */ME.listEmployees();/* Update employee's records */ME.updateEmployee(empID1, 5000);/* Delete an employee from the database */ME.deleteEmployee(empID2);/* List down new list of the employees */ME.listEmployees();}/* Method to CREATE an employee in the database */public Integer addEmployee(String fname, String lname, int salary){Session session = factory.openSession();Transaction tx = null;Integer employeeID = null;try{tx = session.beginTransaction();Employee employee = new Employee(fname, lname, salary);employeeID = (Integer) session.save(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}return employeeID;}/* Method to READ all the employees */public void listEmployees( ){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();List employees = session.createQuery("FROM Employee").list();for (Iterator iterator =employees.iterator(); iterator.hasNext();){Employee employee = (Employee) iterator.next();System.out.print("First Name: " + employee.getFirstName());System.out.print(" Last Name: " + employee.getLastName());System.out.println(" Salary: " + employee.getSalary());}tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}/* Method to UPDATE salary for an employee */public void updateEmployee(Integer EmployeeID, int salary ){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();Employee employee =(Employee)session.get(Employee.class, EmployeeID);employee.setSalary( salary );session.update(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}/* Method to DELETE an employee from the records */public void deleteEmployee(Integer EmployeeID){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();Employee employee =(Employee)session.get(Employee.class, EmployeeID);session.delete(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}}
@Entity@Table(name = "EMPLOYEE")public class Employee {@Id @GeneratedValue@Column(name = "id")private int id;@Column(name = "first_name")private String firstName;@Column(name = "last_name")private String lastName;@Column(name = "salary")private int salary;public Employee() {}public int getId() {return id;}public void setId( int id ) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName( String first_name ) {this.firstName = first_name;}public String getLastName() {return lastName;}public void setLastName( String last_name ) {this.lastName = last_name;}public int getSalary() {return salary;}public void setSalary( int salary ) {this.salary = salary;}}
创建应用类
public class ManageEmployee {private static SessionFactory factory;public static void main(String[] args) {try{factory = new AnnotationConfiguration().configure().//addPackage("com.xyz") //add package if used.addAnnotatedClass(Employee.class).buildSessionFactory();}catch (Throwable ex) {System.err.println("Failed to create sessionFactory object." + ex);throw new ExceptionInInitializerError(ex);}ManageEmployee ME = new ManageEmployee();/* Add few employee records in database */Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);Integer empID3 = ME.addEmployee("John", "Paul", 10000);/* List down all the employees */ME.listEmployees();/* Update employee's records */ME.updateEmployee(empID1, 5000);/* Delete an employee from the database */ME.deleteEmployee(empID2);/* List down new list of the employees */ME.listEmployees();}/* Method to CREATE an employee in the database */public Integer addEmployee(String fname, String lname, int salary){Session session = factory.openSession();Transaction tx = null;Integer employeeID = null;try{tx = session.beginTransaction();Employee employee = new Employee();employee.setFirstName(fname);employee.setLastName(lname);employee.setSalary(salary);employeeID = (Integer) session.save(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}return employeeID;}/* Method to READ all the employees */public void listEmployees( ){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();List employees = session.createQuery("FROM Employee").list();for (Iterator iterator =employees.iterator(); iterator.hasNext();){Employee employee = (Employee) iterator.next();System.out.print("First Name: " + employee.getFirstName());System.out.print(" Last Name: " + employee.getLastName());System.out.println(" Salary: " + employee.getSalary());}tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}/* Method to UPDATE salary for an employee */public void updateEmployee(Integer EmployeeID, int salary ){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();Employee employee =(Employee)session.get(Employee.class, EmployeeID);employee.setSalary( salary );session.update(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}/* Method to DELETE an employee from the records */public void deleteEmployee(Integer EmployeeID){Session session = factory.openSession();Transaction tx = null;try{tx = session.beginTransaction();Employee employee =(Employee)session.get(Employee.class, EmployeeID);session.delete(employee);tx.commit();}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();}finally {session.close();}}}