如何从 JPA 持久性中外部化属性.xml?

2022-09-01 21:21:13

我想将一些休眠配置放在属性文件中,使其无需构建和部署即可编辑。

我试图通过按照创建JPA EntityManager中的说明来解决我的问题,没有持久性.xml配置文件

应用属性:

hibernate.show_sql=true 
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=validate 
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.default_schema=myschema

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<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="pu">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>jdbc/appDatasource</jta-data-source>
      <properties>
         <property name="jboss.entity.manager.factory.jndi.name" value="java:/appEntityManagerFactory"/>
      </properties>
   </persistence-unit>

</persistence>

在初始化代码中,应用程序执行以下序列(查找属性),

Properties props = new Properties();
InputStream is = ClassLoader.getSystemResourceAsStream( "app.properties" );
props.load( is );
Persistence.createEntityManagerFactory( "pu", props );

但失败,并显示错误消息:

 INFO  [SessionFactoryImpl] building session factory
 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
ERROR [STDERR] javax.persistence.PersistenceException: [PersistenceUnit: pu] Unable to build EntityManagerFactory

有没有人知道我的配置可能有什么问题?

版本: JBoss 4.3 接缝: 2.1.2

编辑:

JBoss JNDI 将“pu”作为持久化单元:

persistence.units:ear=app.ear,jar=app.jar,unitName=pu (class: org.hibernate.impl.SessionFactoryImpl)

答案 1

作为当前方法的替代方法,并且由于您正在使用Hibernate,因此您可以使用Hibernate通过使用该属性声明文件来配置JPA,如下所示:hibernate.cfg.xmlhibernate.ejb.cfgfile

<persistence>
 <persistence-unit name="manager1" transaction-type="JTA">
    <jta-data-source>java:/DefaultDS</jta-data-source>
    <properties>
       <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
    </properties>
 </persistence-unit>
</persistence>

我的理解是,它应该只是在类路径上(所以它可能在打包的归档文件之外)。hibernate.cfg.xml

引用


答案 2

刚刚为EclipseLink用户找到了一种所谓的方式。有“eclipselink.persistencexml”,它的默认值为

public static final String ECLIPSELINK_PERSISTENCE_XML_DEFAULT = "META-INF/persistence.xml";

但它不能被覆盖,尽管文档说它可以是...

/**
 * The <code>"eclipselink.persistencexml"</code> property specifies the full
 * resource name to look for the persistence XML files in. If not specified
 * the default value defined by {@link #ECLIPSELINK_PERSISTENCE_XML_DEFAULT}
 * will be used.
 * <p>
 * IMPORTANT: For now this property is used for the canonical model
 * generator but it can later be used as a system property for customizing
 * weaving and application bootstrap usage.
 * <p>
 * This property is only used by EclipseLink when it is locating the
 * configuration file. When used within an EJB/Spring container in container
 * managed mode the locating and reading of this file is done by the
 * container and will not use this configuration.
 */

推荐