下载 Hibernate Core 发行版。设置类路径(在你最喜欢的 IDE 中创建新项目后)
<project ...>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-core-version}</version>
</dependency>
</dependencies>
</project>
所有必需的依赖项,如 hibernate-core 和 hibernate-annotations,都将瞬态拖动过来。
我们建议你使用 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 网站下载它,并在类路径中添加 hibernate-search.jar
及其依赖项。或者,在 pom.xml
中添加以下依赖项。
<project>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>${hibernate-search-version}</version>
</dependency>
...
</dependencies>
...
</project>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="sample">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
以下是
文件更完整的一个示例persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<mapping-file>ormap.xml</mapping-file>
<jar-file>MyApp.jar</jar-file>
<class>org.acme.Employee</class>
<class>org.acme.Person</class>
<class>org.acme.Address</class>
<shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
名称
事务类型
提供者
提供者是 EJB 持久性提供者的完全限定类名称。如果你不使用多个 EJB3 实现,你无需定义它。当使用 EJB 持久性的多个供应商实现时需要这样做。
jta-data-source
, non-jta-data-source
这是位于 javax.sql.DataSource 的 JNDI 名称。在没有 JNDI 可用数据源的情况下运行时,必须使用 Hibernate 特定属性指定 JDBC 连接(参见下文)。
映射文件
jar 文件
<jar-file>file:/home/turin/work/local/lab8/build/classes</jar-file>
排除未列出的类
类
默认情况下,如果用 @Cacheable
做了注解,那么实体将被选为二级缓存。但是,您可以
不幸的是,DDL
不是标准模式(尽管非常有用),您将无法将其放入 <validation-mode>
中。要使用它,请添加一个常规属性
<property name="javax.persistence.validation.mode">
ddl
</property>
使用这种方法,您可以混合 ddl 和 callback 模式
<property name="javax.persistence.validation.mode">
ddl, callback
</property>
properties
properties 元素用于指定供应商特定的属性。这是您定义 Hibernate 特定配置的地方。这也是您必须指定 JDBC 连接信息的地方。
以下是 JPA 2 标准属性的列表。请务必参考 Hibernate Core 文档以查看 Hibernate 的特定属性。
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
//or
Map<String, Object> configOverrides = new HashMap<String, Object>();
configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");
EntityManagerFactory programmaticEmf =
Persistence.createEntityManagerFactory("manager1", configOverrides);
第一个版本等同于带有“空映射”的第二个版本,映射版本是将优先于在“persistence.xml”文件中定义的任何属性的一组覆盖项。在第 2.2.1 节“打包”中定义的所有属性均可传递给 createEntityManagerFactory
方法,另外还有一些其它属性:
javax.persistence.provider
用来定义所用的提供程序类
javax.persistence.transactionType
用来定义所用的事务类型(JTA
或 RESOURCE_LOCAL
)
javax.persistence.jtaDataSource
用来定义 JNDI 中的 JTA 数据源名称
javax.persistence.nonJtaDataSource
用来定义 JNDI 中的非 JTA 数据源名称
javax.persistence.lock.timeout
以毫秒为单位的悲观锁超时时间 (Integer
或 String
)
javax.persistence.query.timeout
以毫秒为单位的查询超时时间 (Integer
或 String
)
javax.persistence.sharedCache.mode
对应于在第 2.2.1 节“打包”中定义的 share-cache-mode
元素。
javax.persistence.validation.mode
对应于在第 2.2.1 节“打包”中定义的 validation-mode
元素。
调用 Persistence.createEntityManagerFactory()
时,持久性实现会使用 ClassLoader.getResource("META-INF/persistence.xml")
方法,在您的类路径中查找任何 META-INF/persistence.xml
文件。实际上,Persistence
类将查看类路径中提供的所有持久性提供程序,并逐个询问它们是否负责创建实体管理器工厂 manager1
。在这些资源的列表中,每个提供程序都会尝试找到一个实体管理器,该实体管理器的名称与您在命令行中指定的名称匹配,且与在 persistence.xml 文件中指定的内容匹配(当然,提供程序 element
必须与当前的持久性提供程序匹配)。如果找不到具有正确名称的 persistence.xml,或者如果找不到预期的持久性提供程序,则会引发 PersistenceException
。
除了 Hibernate 系统级设置外,Hibernate 中提供的所有属性都可以设置在 persistence.xml 文件的 properties
元素中,或者作为您传递给 createEntityManagerFactory()
的映射中的覆盖项。有关完整列表,请参阅 Hibernate 参考文档。但是,EJB3 提供程序中还有几个属性可用。
请注意,您可以在同一配置中混用 XML <class>
声明和 hibernate.ejb.cfgfile
用法。请注意潜在的冲突。在 persistence.xml
中设置的属性会覆盖在已定义的 hibernate.cfg.xml
中的属性。
很重要的一点是不要覆盖 hibernate.transaction.factory_class
,Hibernate EntityManager 会根据 EntityManager 类型(即 JTA
与 RESOURSE_LOCAL
)自动设置合适的交易工厂。如果您在 Java EE 环境中工作,那么您可能希望设置 hibernate.transaction.manager_lookup_class
。
以下是在 Java SE 环境中的典型配置
<persistence>
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<class>org.hibernate.ejb.test.Cat</class>
<class>org.hibernate.ejb.test.Distributor</class>
<class>org.hibernate.ejb.test.Item</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/
<property name="hibernate.max_fetch_depth" value="3"/>
<!-- cache configuration -->
<property name="hibernate.ejb.classcache.org.hibernate.ejb.test.Item" value="read-write"/>
<property name="hibernate.ejb.collectioncache.org.hibernate.ejb.test.Item.distributors" value="read-write, RegionName"/>
<!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file -->
<!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ -->
</properties>
</persistence-unit>
</persistence>
为了简化编程配置,Hibernate Entity Manager 提供了一个专有 API。此 API 与 Configuration
API 非常类似并且共享相同概念:Ejb3Configuration
。有关如何使用它的更详细的信息,请参阅 JavaDoc 和 Hibernate 参考指南。
TODO:对某些 API(如 setDatasource())进行更详细描述
Ejb3Configuration cfg = new Ejb3Configuration();
EntityManagerFactory emf =
cfg.addProperties( properties ) //add some properties
.setInterceptor( myInterceptorImpl ) // set an interceptor
.addAnnotatedClass( MyAnnotatedClass.class ) //add a class to be mapped
.addClass( NonAnnotatedClass.class ) //add an hbm.xml file using the Hibernate convention
.addRerousce( "mypath/MyOtherCLass.hbm.xml ) //add an hbm.xml file
.addRerousce( "mypath/orm.xml ) //add an EJB3 deployment descriptor
.configure("/mypath/hibernate.cfg.xml") //add a regular hibernate.cfg.xml
.buildEntityManagerFactory(); //Create the entity manager factory
请注意,如果未启用安全功能,将移除 JACC*EventListeners
。
您可以通过属性(请参阅 配置和启动)或通过 ejb3configuration.getEventListeners()
API 来配置事件侦听器。
实体管理器工厂应被视为不可变配置持有者,定义为指向单个数据源并映射一组已定义实体。这是创建和管理 EntityManager
的入口点。 Persistence
类是用于创建实体管理器工厂的引导类。
// Use persistence.xml configuration
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1")
EntityManager em = emf.createEntityManager(); // Retrieve an application managed entity manager
// Work with the EM
em.close();
...
emf.close(); //close at application end
版权所有 © 2005 Red Hat Inc.以及作者