2.2 配置与引用
2.2.1 配置
无论是一个应用服务器还是单独的一个应用服务程序都是在一个持久化的工程中去进行实体的管理配置。一个持久化的工程必须在工程的META-INF文件夹下配置一个persistence.xml文件。在你的持久化的工程中所有的注解类(ie 具有一个@Entity注解)、所有的包以及所有的Hibernate的hbm.xml的配置文件都要添加到persistence.xm文件的持久化单元配置中。因此默认情况下,您的persistence.xm文件至少应该包含以下内容:
<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_1_0.xsd" version="1.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_1_0.xsd" version="1.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> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence>
name
(属性)每一个实体管理器必须有一个名称
transaction-type
(属性)使用的事物类型。有两种:JTA和RESOURCE_LOCAL(在J2EE环境中默认是JTA,而在J2SE环境中默认是RESOURCE_LOCAL)。当使用的是jta数据源时,默认的事物类型是JTA,如果使用的不是jta数据源时,默认的事物类型就是RESOURCE_LOCAL。
provider
provider是EJB持久化提供者的固定的名称。假如你没有用到EJB3来实现你的工作的时候,就没有必要去定义这个。如果你的EJB是由某个供应商实现的,那就有必要去定义这个标签了。
jta-data-source,non-jta-data-source
这是javax.sql.DataSource所在的JNDI名称。当没有可用的JNDI数据源运行时,必须指定使用Hibernate特定属性的JDBC连接(见下文)。 mapping-file 类元素指定一个你将映射EJB3标准的XML映射文件。这个文件必须在classpath中。根据EJB3规范,Hibernate的EntityManager中会尝试加载位于在META_INF/ orm.xml中的jar文件映射文件。当然,任何显式的映射文件将被加载。事实上,你可以在映射文件中的任意元素,即提供任何XML文件。无论是hbm文件或EJB3的部署描述符。
jar-file
这个jar-files里的元素需要一个特殊饿jar文件去解析。所有正确的注解类、注解包、以及hbm.xml文件都将被添加到持久化单元中去。这些元素主要用于J2EE环境中去。如果在J2SE中使用上述内容,那就不便捷了,那就需要一个绝对的路径了。您可以直接指向一个目录。
<jar-file>file:/home/turin/work/local/lab8/build/classes</jar-file>
exclude-unlisted-classes 不检查注解类的主要jar文件。明确持久化单元中的类。
class class元素指定一个你将映射的固定的类名。默认情况下所有正确注解的类、所有的hbm.xml文件都被添加到持久化单元中。你可以通过类的元素添加一些额外的实体类。作为一个扩展的规范,你可以在class元素中添加一个包名(例如:<class>org.hibernate.eg</class>)。注意,这个包将包含被定义包级别的所有元数据。它将不包括该包下的所有的类。 properties properties元素用于指定供应商特定的属性。在这里您将定义您的hibernate的特定配置。你也可以在这儿配置你的jdbc的连接信息。 因为EJB3规范要求架构验证,所以一定要按语法来定义持久化元素。如果systemId以persistence_1_0.xsd结束时,Hibernate entityManager将使用嵌入在Hibernate-entityManager.jar中的版本。在没有联网的情况下也能被成功的处理。
<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_1_0.xsd" version="1.0">
2.2.2引用
EJB3规范说明 通过EntityManagerFactory和EntityManager来定义一个引用的程序。这个启动的类就是javax.persistence.Persistence。例如:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1"); //or Map configOverrides = new HashMap(); configOverrides.put("hibernate.hbm2ddl.auto", "create-drop"); EntityManagerFactory programmaticEmf = Persistence.createEntityManagerFactory("manager1", configOverrides);
第一个版本的调用等同于第二个版本带一个空的类型为Map的参数。第二个版本的Map参数内容给将覆盖定义在persistence.xml中定义的相同的参数内容。在Map参数中可以使用的EJB3的属性:
-
hibernate.ejb.classcache 类缓存策略
- hibernate.ejb.collectioncache.<collectionrole> 集合缓存策略
- hibernate.ejb.cfgfile 配置hibernate的xml配置文件
- hibernate.archive.autodetection 确定哪些元素是自动由Hibernate实体管理器在解析
- hibernate.ejb.interceptor 一个可选的Hibernate拦截器。拦截器实例由所有会话实例共享。该拦截器必须实现org.hibernate.Interceptor并且有一个默认的无参构造函数。这个属性不能与hibernate.ejb.interceptor.session_scoped结合。
- hibernate.ejb.interceptor.session_scoped 一个可选的Hibernate拦截器。拦截器的实例是针对特定的Session实例(因而可以是非线程安全的)。该拦截器必须实现org.hibernate.Interceptor并且有一个默认的无参构造函数。这个属性不能与hibernate.ejb.interceptor结合。
- hibernate.ejb.naming_strategy 一个可选的命名策略。使用默认的命名策略是EJB3NamingStrategy。您也可以考虑DefaultComponentSafeNamingStrategy。
- hibernate.ejb.event.<eventtype> 事件侦听器列表中一个给定的事件类型。事件侦听器的列表是一个逗号分隔的完全限定类名列表(如 hibernate.ejb.event.pre-load com.acme.SecurityListener,com.acme.AuditListener)。
- hibernate.ejb.use_class_enhancer 在部署时是否使用应用程序服务器类增强(默认为false)。
- hibernate.ejb.discard_pc_on_close 如果为true,持久化上下文将被丢弃(调用该方法,考虑clear().否则持久化上下文会保持活跃状态持续下去,直到事物完成:所有的对象将保持管理,任何变化都会与数据库同步(默认情况下,即等待事物完成)。
请注意,您可以混合使用XML<class>定义和hibernate.ejb.cfgfile 使用相同的配置。应该注意到发生冲突的可能性。在persistence.xml中设置的属性将覆盖在hibernate.cfg.xml中定义的之一。
提醒
不重写hibernate.transaction.factory_class是很重要的。Hibernate EntityManager将根据EntityManager 的类型自动的设置适当的事物工场。如果你的环境是J2EE环境,你有可能得设置hibernate.transaction.manager_lookup_class。
下面是在J2SE环境中的典型配置:
<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="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:hsqldb:."/> <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的实体管理器提供一个专有的API。这个API与Configuration API非常相似的,并拥有相同的概念:Ejb3Configuration。请参考Javadoc和Hibernate参考指南,了解更详细的信息如何使用它。
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