1. 获得Hibernate (Obtaining Hibernate)
1.1. Hibernate模块工件(The Hibernate Modules/Artifacts)
Hibernate的功能被分成多个模块/工件,旨在隔离依赖关系(模块化)
模块名称 | 模块作用 |
---|---|
hibernate-core | 主(核心)Hibernate模块。定义其ORM功能和API以及各种集成SPI。 |
hibernate-envers | Hibernate的历史实体版本控制功能。 |
hibernate-spatial | 为Hibernate的Spatial / GIS数据类型支持 |
hibernate-osgi | Hibernate支持在OSGi容器中运行。 |
hibernate-c3p0 | 将C3P0连接池库集成到Hibernate中。 |
hibernate-hikaricp | 将HikariCP连接池库集成到Hibernate中。 |
hibernate-proxool | 将Proxool连接池库集成到Hibernate中 |
hibernate-jcache | 将JCache缓存规范集成到Hibernate中,使任何兼容的实现都可以成为第二级缓存提供程序。 |
hibernate-ehcache | 将Ehcache缓存库作为第二级缓存提供程序集成到Hibernate中。 |
hibernate-infinispan | 将Infinispan缓存库作为第二级缓存提供程序集成到Hibernate中。 |
1.2. 发行包下载(Release Bundle Downloads)
https://sourceforge.net/projects/hibernate/files/hibernate-orm/
- The lib/required/ directory contains the hibernate-core jar and all
of its dependencies. All of these jars are required to be available
on your classpath no matter which features of Hibernate are being
used. - The lib/envers directory contains the hibernate-envers jar and all of
its dependencies (beyond those in lib/required/ and lib/jpa/). - The lib/spatial/ directory contains the hibernate-spatial jar and all
of its dependencies (beyond those in lib/required/) - The lib/osgi/ directory contains the hibernate-osgi jar and all of
its dependencies (beyond those in lib/required/ and lib/jpa/) - The lib/jpa-metamodel-generator/ directory contains the jar needed
for generating the Criteria API type-safe Metamodel. - The lib/optional/ directory contains the jars needed for the various
connection pooling and second-level cache integrations provided by
Hibernate, along with their dependencies.
1.3. Maven仓库组件(Maven Repository Artifacts)
Hibernate工件的权威存储库是JBoss Maven存储库。Hibernate工件作为自动化作业的一部分同步到Maven Central(可能会发生一些小的延迟)。负责JBoss Maven存储库的团队维护着许多包含重要信息的Wiki页面:
- http://community.jboss.org/docs/DOC-14900 - General information about the repository.
- http://community.jboss.org/docs/DOC-15170 - Information about setting up the JBoss repositories in order to do development work on JBoss projects themselves.
- http://community.jboss.org/docs/DOC-15169 - Information about setting up access to the repository to use JBoss projects as part of your own software.
Hibernate ORM工件发布在org.hibernate groupId下(maven的groupId)
2. 本地使用Hibernate API和hbm.xml映射的教程(Tutorial Using Native Hibernate APIs and hbm.xml Mapping)
This tutorial is located within the download bundle under basic/.
目标
- 引导一个Hibernate SessionFactory
- 使用Hibernate映射文件(hbm.xml)提供映射信息
- 使用Hibernate本机API
2.1. Hibernate配置文件(The Hibernate configuration file)
首先通过hibernate.cfg.xml文件定义Hibernate的配置信息。
connection.driver_class,connection.url,connection.username和connection.password 元素定义JDBC连接信息。这些教程利用了H2内存数据库,因此这些属性的值都特定于在其内存模式下运行H2。
connection.pool_size用于配置Hibernate内置连接池中的连接数。
Dialect属性指定了Hibernate可以与之交谈的特定SQL变体。(在大多数情况下,Hibernate能够正确确定要使用的方言。如果您的应用程序针对多个数据库,则这特别有用。)
hbm2ddl.auto属性允许直接在数据库中自动生成数据库模式。
提示:内置的Hibernate连接池绝不用于生产用途。它缺少可用于生产环境的连接池上发现的几个功能。
最后,将持久性类的映射文件添加到配置中。元素的resource属性使Hibernate尝试使用java.lang.ClassLoader查找将该映射作为类路径资源进行定位。
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</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">create</property>
<mapping resource="org/hibernate/tutorial/hbm/Event.hbm.xml"/><!-- Need to create and edit-->
</session-factory>
</hibernate-configuration>
2.2. Java实体类(The entity Java class)
实体类是org.hibernate.tutorial.hbm.Event
关于实体类的注意事项
- 此类使用标准JavaBean命名约定作为属性getter和setter方法,以及领域。尽管这是推荐的设计,但不是必需的。
- 无参数构造函数(也是JavaBean约定)是所有持久化类的要求。Hibernate需要通过Java反射为您准备对象。构造函数可以是私人的。但是,运行时代理生成和有效数据检索就需要包或公共可见性而无需字节码检测。
package org.hibernate.tutorial.hbm;
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Event() {
// this form used by Hibernate
}
public Event(String title, Date date) {
// for application use, to create new events
this.title = title;
this.date = date;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2.3. 映射文件(The mapping file:*.hbm.xml)
映射文件是类路径资源org / hibernate / tutorial / hbm / Event.hbm.xml(如hibernate.cfg.xml所述)。
Hibernate使用映射元数据来确定如何加载和存储持久类的对象。Hibernate映射文件是为Hibernate提供此元数据的一种选择。在这里插入代码片
例子1.类映射元素
<class name="Event" table="EVENTS">
...
</class>
class映射元素的功能
- name属性(此处与来自package中包含的元素的package属性组合在一起)为要定义为实体的类的FQN命名。
- table属性为包含该实体数据的数据库表命名。
现在,将Event类的实例映射到EVENTS数据库表中的行。
例子2. id映射元素
<id name="id" column="EVENT_ID">
...
</id>
Hibernate使用元素命名的属性来唯一标识表中的行。
此处的元素将EVENT_ID列命名为EVENTS表的主键。它还将Event类的id属性标识为包含标识符值的属性。生成器元素通知Hibernate使用哪种策略为该实体生成主键值。本示例使用简单的递增计数。
id元素不需要映射到表的实际主键列,但这是常规约定。在Hibernate中映射的表甚至不需要定义主键。但是,强烈建议所有模式都定义适当的参照完整性。因此,id和主键在整个Hibernate文档中可互换使用。
例子3.属性映射元素
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
两个property元素声明Event类的其余两个持久属性:date和title。日期属性映射包括列属性,但标题不包括。在没有列属性的情况下,Hibernate使用属性名称作为列名称。这适用于标题,但是由于date是大多数数据库中的保留关键字,因此您需要为列名指定一个非保留字。
标题映射也缺少类型属性。映射文件中声明和使用的类型既不是Java数据类型也不是SQL数据库类型。相反,它们是Hibernate映射类型,它们是在Java和SQL数据类型之间转换的转换器。如果未在映射中指定type属性,则Hibernate尝试自动确定正确的转换和映射类型,方法是使用Java反射来确定已声明属性的Java类型,并使用该Java类型的默认映射类型。
在某些情况下,自动检测可能未选择您期望或需要的默认值,如date属性所示。Hibernate无法知道类型为java.util.Date的属性是否应映射到SQL DATE,TIME或TIMESTAMP数据类型。通过将属性映射到时间戳转换器来保留完整的日期和时间信息,该时间戳转换器标识转换器类org.hibernate.type.TimestampType。
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.tutorial.hbm">
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
2.4. 范例代码(Example code)
为了易于使用,这些教程中的示例以JUnit测试的形式呈现。这种方法的优点之一是setUp和tearDown大致说明了如何在应用程序启动时创建org.hibernate.SessionFactory并在应用程序生命周期结束时将其关闭。
例子4.获得org.hibernate.SessionFactory
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.c