How to externalize properties from JPAs persistence.xml?

I would like to put some hibernate configuration in the properties file to make it editable without assembly and deployment.

I tried to solve my problem by following the Create JPA EntityManager instructions without the 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 with the error message does not appear:

  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 

Can anyone understand what might be wrong in my configuration?

Versions: JBoss 4.3 Seam: 2.1.2

EDIT:

JBoss JNDI enters "pu" as the unit of continuity:

 persistence.units:ear=app.ear,jar=app.jar,unitName=pu (class: org.hibernate.impl.SessionFactoryImpl) 
+22
java java-ee hibernate jpa seam
Oct 14 '10 at 16:29
source share
4 answers

As an alternative to your current approach, and since you use Hibernate, you can use Hibernate to configure JPA by declaring the hibernate.cfg.xml file using the hibernate.ejb.cfgfile property, for example:

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

I understand that hibernate.cfg.xml should only be in the classpath (so it can be outside the packed archive).

References

+9
Oct. 16 2018-10-10
source share

Just found a way for EclipseLink users. There is eclipselink.persistencexml ", the default value

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

but it cannot be redefined, although the documents say that it could 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. */ 
+2
Feb 15 '12 at 7:25
source share

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

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

+1
Nov 19 '13 at 6:15
source share

If you use Spring to manage and administer the object manager, then you can implement org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor and pass external properties. I could successfully squeeze out all the properties from persistence.xml using this.

+1
Feb 11 '16 at 10:53 on
source share



All Articles