如何从 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)