如何外化JPAs persistence.xml中的属性?

时间:2022-04-06 20:00:20

I would like to put some of the hibernate configuration in a property file to make it editable without build and deploy.

我想将一些hibernate配置放在属性文件中,以使其在没有构建和部署的情况下可编辑。

I tried to solve my problem by following the instructions from Create JPA EntityManager without persistence.xml configuration file

我尝试通过遵循Create JPA EntityManager中没有persistence.xml配置文件的指示来解决我的问题

app.properties:

app.properties:

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

persistence.xml

persistence.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>

In the initialization code the application executes the following sequence, (which finds the properties),

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

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

but fails with the error message:

但失败并显示错误消息:

 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

Does anyone have an idea what could be wrong with my configuration?

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

Versions: JBoss 4.3 Seam: 2.1.2

版本:JBoss 4.3 Seam:2.1.2

EDIT:

编辑:

JBoss JNDI enlists "pu" as persistence unit:

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

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

4 个解决方案

#1


9  

As an alternative to your current approach and since you're using Hibernate, you could use Hibernate to configure JPA by declaring a hibernate.cfg.xml file using the hibernate.ejb.cfgfile property, like this:

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

<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>

My understanding is that the hibernate.cfg.xml is just supposed to be on the classpath (so it could be outside the packaged archive).

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

References

#2


2  

Just found a an alleged way for EclipseLink users. There is "eclipselink.persistencexml" which has a default value of

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

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

but it can't be overridden although the docs say it can be...

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

/**
 * 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.
 */

#3


1  

I used this mechanism, seems to work for most of the properties, had issues with non-jta-data-source.

我使用这种机制,似乎适用于大多数属性,与非jta-data-source有问题。

http://www.eclipse.org/eclipselink/api/2.4/index.html?org/eclipse/persistence/config/PersistenceUnitProperties.html

http://www.eclipse.org/eclipselink/api/2.4/index.html?org/eclipse/persistence/config/PersistenceUnitProperties.html

#4


1  

If you are using Spring to manage and inject entity manager, then it is possible to implement org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor and pass on external properties. I could successfully externalize all properties from persistence.xml using this.

如果您使用Spring来管理和注入实体管理器,那么可以实现org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor并传递外部属性。我可以使用它成功地从persistence.xml外部化所有属性。

#1


9  

As an alternative to your current approach and since you're using Hibernate, you could use Hibernate to configure JPA by declaring a hibernate.cfg.xml file using the hibernate.ejb.cfgfile property, like this:

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

<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>

My understanding is that the hibernate.cfg.xml is just supposed to be on the classpath (so it could be outside the packaged archive).

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

References

#2


2  

Just found a an alleged way for EclipseLink users. There is "eclipselink.persistencexml" which has a default value of

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

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

but it can't be overridden although the docs say it can be...

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

/**
 * 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.
 */

#3


1  

I used this mechanism, seems to work for most of the properties, had issues with non-jta-data-source.

我使用这种机制,似乎适用于大多数属性,与非jta-data-source有问题。

http://www.eclipse.org/eclipselink/api/2.4/index.html?org/eclipse/persistence/config/PersistenceUnitProperties.html

http://www.eclipse.org/eclipselink/api/2.4/index.html?org/eclipse/persistence/config/PersistenceUnitProperties.html

#4


1  

If you are using Spring to manage and inject entity manager, then it is possible to implement org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor and pass on external properties. I could successfully externalize all properties from persistence.xml using this.

如果您使用Spring来管理和注入实体管理器,那么可以实现org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor并传递外部属性。我可以使用它成功地从persistence.xml外部化所有属性。