hibernate 如何从JPApersistence.xml外部化属性?

iqjalb3h  于 2023-08-06  发布在  其他
关注(0)|答案(5)|浏览(119)

我想把一些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 Seam:2.1.2
编辑:
JBoss JNDI将“pu”登记为持久性单元:

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

niwlg2el

niwlg2el1#

作为当前方法的替代方案,由于您使用的是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>

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

参考资料

  • Hibernate Entity Manager参考指南
  • 表2.1. Hibernate实体管理器的特定属性
ubbxdtey

ubbxdtey2#

刚刚发现了一个所谓的方式为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.
 */

e37o9pze

e37o9pze3#

我使用了这种机制,似乎对大多数属性都有效,但对非jta数据源有问题。
http://www.eclipse.org/eclipselink/api/2.4/index.html?org/eclipse/persistence/config/PersistenceUnitProperties.html

w9apscun

w9apscun4#

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

watbbzwu

watbbzwu5#

**persistence.xml**
            
            <?xml version="1.0" encoding="UTF-8"?>
            <persistence version="2.2"
                xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
                <persistence-unit name="jsf-maven-project">
                    <properties>
                       <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
                    </properties>
                </persistence-unit>
            </persistence>
           
**hibernate.cfg.xml**
            <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
          <hibernate-configuration>
            <session-factory>
              <property name="connection.driver_class">org.postgresql.Driver</property>
              <property name="connection.url">jdbc:postgresql://localhost:5432/testdb</property>
              <property name="connection.username">postgres</property>
              <property name="connection.password">Test@123</property>
              <property name="connection.pool_size">10</property>
              <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
              <property name="current_session_context_class">thread</property>
              <property name="show_sql">true</property>
              <property name="format_sql">true</property>
              <property name="hbm2ddl.auto">update</property>
              <!-- mapping  class="com.sample.Book" / -->
          </session-factory>
        </hibernate-configuration>
    
**HibernateUtil.java**
            public static EntityManager getEntityManager() {
                    if (entityManagerFactory == null) {
                        entityManagerFactory = Persistence.createEntityManagerFactory("jsf-maven-project");
                    }
                    return entityManagerFactory.createEntityManager();
                }

字符串
我用hibernate JPA创建JSF项目,这个解决方案对我很有效。

相关问题