我想将一些hibernate配置放在属性文件中,以便在没有构建和部署的情况下使其可编辑。
我尝试按照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>
在初始化代码中,应用程序执行以下序列(找到属性),
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)
答案 0 :(得分:9)
作为当前方法的替代方案,并且由于您正在使用Hibernate,您可以使用Hibernate通过使用hibernate.cfg.xml
属性声明hibernate.ejb.cfgfile
文件来配置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>
我的理解是hibernate.cfg.xml
应该在类路径上(所以它可能在打包存档之外)。
答案 1 :(得分: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.
*/
答案 2 :(得分:1)
我使用这种机制,似乎适用于大多数属性,与非jta-data-source有问题。
答案 3 :(得分:1)
如果您使用Spring来管理和注入实体管理器,那么可以实现org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor并传递外部属性。我可以使用它来成功地从persistence.xml中外部化所有属性。