从 Hibernate 网站下载并解压缩 Hibernate Core 发行版。Hibernate 3.5 及更高版本包含 Hibernate Annotations。
或者,在您的依赖关系管理器(如 Maven 或 Ivy)中添加以下依赖关系。这里有一个示例
<project ...>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate-core-version}</version>
</dependency>
</dependencies>
</project>
或者,将您的 pom.xml 导入您最喜欢的 IDE,让依赖关系自动解析,
这是包含 JPA 2.0 API 的 JAR,它完全符合规范并通过 TCK 签名测试。您通常不需要在 Java EE 6 应用程序服务器(例如 JBoss AS 6)中部署应用程序时使用它。
我们建议您使用 Hibernate Validator 和 Bean Validation 规范功能,因为它与 Java Persistence 2 的集成已标准化。从 Hibernate 网站下载 Hibernate Validator 4 或更高版本,并添加 hibernate-validator.jar
和 validation-api.jar
到您的类路径中。或者,在您的 pom.xml
中添加以下依赖关系。
<project>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator-version}</version>
</dependency>
...
</dependencies>
...
</project>
如果您希望使用 Hibernate Search,请从 Hibernate 网站下载它并添加 hibernate-search.jar
及其依赖关系到您的类路径中。或者,在您的 pom.xml
中添加以下依赖关系。
<project>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>${hibernate-search-version}</version>
</dependency>
...
</dependencies>
...
</project>
我们建议您使用 JPA 2 API 引导 Hibernate(有关详细信息,请参阅 Hibernate EntityManager 文档)。如果您使用 Hibernate Core 及其本机 API,请继续阅读。
如果您自己引导 Hibernate,请务必使用 AnnotationConfiguration
类,而不是 Configuration
类。以下是一个使用(旧版)HibernateUtil
方法的示例
package hello;
import org.hibernate.*;
import org.hibernate.cfg.*;
import test.*;
import test.animals.Dog;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession()
throws HibernateException {
return sessionFactory.openSession();
}
}
这里有趣的是使用 AnnotationConfiguration
。包和带注释的类在常规 XML 配置文件中声明(通常为 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>
<mapping package="test.animals"/>
<mapping class="test.Flight"/>
<mapping class="test.Sky"/>
<mapping class="test.Person"/>
<mapping class="test.animals.Dog"/>
<mapping resource="test/animals/orm.xml"/>
</session-factory>
</hibernate-configuration>
请注意,您可以混用传统的 hbm.xml 用法和注释方法。资源元素既可以是 hbm 文件,也可以是 EJB3 XML 部署描述符。对于您的配置流程来说,这种区别是透明的。
或者,您可以使用编程 API 定义带注释的类和程序包
sessionFactory = new AnnotationConfiguration() .addPackage("test.animals") //the fully qualified package name .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .addAnnotatedClass(Person.class) .addAnnotatedClass(Dog.class) .addResource("test/animals/orm.xml")
.configure()
.buildSessionFactory();
除了这个启动例程更改或在配置文件中更改以外,您使用 Hibernate API พร้อม注释的方式没有其他区别。您可以对其他属性使用您最喜欢的配置方法(hibernate.properties
、hibernate.cfg.xml
、编程 API 等)。
您可以使用相同的 SessionFactory
混用带注释的持久类和经典 hbm.cfg.xml
声明。但是,您不能多次声明一个类(无论是带注释声明,还是通过 hbm.xml 声明)。您也不能在实体层次结构中混用配置策略(hbm 和注释)。
为了简化从 hbm 文件迁移到注释的过程,配置机制会检测注释和 hbm 文件之间的映射重复。然后,在逐类基础上,HBM 文件优先于带注释的元数据。您可以使用 hibernate.mapping.precedence
属性更改优先级。默认值为 hbm, class
,将其更改为 class, hbm
将在发生冲突时使带注释的类优先于 hbm 文件。
Hibernate 注解使用Simple Logging Facade for Java (SLF4J) 以记录各种系统事件。根据所选的绑定,SLF4J 可以将日志输出直接发送到以下几种日志记录框架(NOP、Simple、log4j 版本 1.2、JDK 1.4 日志记录、JCL 或 logback)。为了正确设置日志记录功能,你需要将 slf4j-api.jar
与首选绑定的 jar 文件一同放置到类路径中 - 对于 Log4J 来说,该文件为 slf4j-log4j12.jar
。有关更多详情,请参阅 SLF4J 文档。
对于 Hibernate 注解来说,感兴趣的日志记录类别有
有关其他类配置,请参阅日志记录(位于 Hibernate Core 文档中)。
版权所有 © 2004 Red Hat Inc. 和不同的作者