如何从JPA persistence.xml中外部化属性? [英] How to externalize properties from JPAs persistence.xml?

查看:87
本文介绍了如何从JPA persistence.xml中外部化属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我试图通过遵循来自创建没有persistence.xml配置文件的JPA EntityManager

的说明

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

 <?xml version =1.0encoding =UTF-8?> 
<! - dev配置文件的持久性部署描述符 - >
< 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>
<属性>
< property name =jboss.entity.manager.factory.jndi.namevalue =java:/ appEntityManagerFactory/>
< / properties>
< / persistence-unit>

< /持久性>

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

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

但失败并显示错误讯息:

 INFO [SessionFactoryImpl]构建会话工厂
INFO [SessionFactoryObjectFactory]没有将工厂绑定到JNDI,没有配置JNDI名称
ERROR [STDERR] javax.persistence.PersistenceException: PersistenceUnit:pu]无法构建EntityManagerFactory

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

版本:JBoss 4.3
Seam:2.1.2

编辑:



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

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


解决方案

作为当前方法的替代方案,并且由于您使用的是Hibernate,因此您可以使用Hibernate通过声明 hibernate.cfg.xml 文件使用 hibernate。 ejb.cfgfile 属性,如下所示:

 < persistence> 
< persistence-unit name =manager1transaction-type =JTA>
< jta-data-source> java:/ DefaultDS< / jta-data-source>
<属性>
< property name =hibernate.ejb.cfgfilevalue =/ hibernate.cfg.xml/>
< / properties>
< / persistence-unit>
< /余辉>

我的理解是 hibernate.cfg.xml

参考文献




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

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

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

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

EDIT:

JBoss JNDI enlists "pu" as persistence unit:

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

解决方案

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:

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

References

这篇关于如何从JPA persistence.xml中外部化属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆