5 Replies Latest reply: Aug 3, 2010 1:59 AM by PhHein RSS

    Persistence.xml location

      I have an application, and want to be able to point it at a different db instance (same schema,etc).
      I could obviously change the persistence.xml in the jar, but this is cumbersome, and not very easy to do.

      Is it possible to place the persistence.xml outside of the jar and reference it in the classpath somehow?

      Andy Morton
        • 1. Re: Persistence.xml location
          is this not possible?
          • 2. Re: Persistence.xml location
            If you have a proper JEE application is deployed in some application server (eg Glassfish), simply don't put any database configuration in the persistence.xml and use a JDBC Connection configured in the application server console. Just include the data source name in the persistence.xml and configure your JDBC data source to have that name in the JNDI. For example we have:
            at the top of our persistence.xml, and that's it. The logical name is found in JNDI and the database can be altered externally.

            For our "test" environments, these can run against a variety of different databases, outside of a JEE server. So we use Spring to set up the objects necessary for JPA to work against the selected database for each test. This also has the advantage of allowing us to configure a special persistence.xml file used only for testing. For example, this Spring config causes our tests to run against an HSQL in-memory database (Our tests are subclassed from Spring's AbstractJpaTests class) :-
            <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                                       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                                       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
                 <tx:annotation-driven  transaction-manager="h8TransactionManager"/> 
                 <alias alias="dataSource" name="hsqlDataSource"/>
                 <bean id="hsqlDataSource"
                      <property name="driverClassName"      value="org.hsqldb.jdbcDriver"/>           
                      <property name="url"                value="jdbc:hsqldb:mem:testdb"/>           
                      <property name="username"           value="sa"/>           
                      <property name="password"           value=""/>     
                 <bean id="h8JPAVendorAdapter"
                      <property name="showSql"           value="false"/>
                      <property name="generateDdl"           value="true"/>
                      <property name="database"           value="HSQL"/>
                 <alias alias="entityManagerFactory" name="h8EntityManagerFactory"/>
                 <bean id="h8EntityManagerFactory" 
                     <property name="dataSource"              ref="dataSource"/>
                     <property name="jpaVendorAdapter"        ref="h8JPAVendorAdapter"/>
                     <property name="persistenceXmlLocation"  value="classpath:/test/jpa/persistence.xml"/>
                     <property name="persistenceUnitName"     value="PUNAME"/>
                 <alias alias="transactionManager" name="h8TransactionManager"/>
                 <bean id="h8TransactionManager" 
                      <property name="entityManagerFactory"      ref="h8EntityManagerFactory"/>
                      <property name="dataSource"           ref="dataSource"/>
                 <bean id="jpaType" class="biz.wss.jpa.JPAType" factory-method="valueOf">     
                     <constructor-arg value="HIBERNATE"/>
            Hope that helps a bit...

            Edited by: edrandall on Mar 20, 2009 9:24 AM
            • 3. Re: Persistence.xml location
              Hi, I have a similar problem but i can't solve it by putting jndi configuration of datasources in persistence.xml file.
              The problem is that my application is deployed in three different application servers (development, test and production) and the jndi names of the corresponding data sources are different in each server.
              So can I put the persistence.xml file in another location in the classpath outside of META-INF directory and outside of the jar file?
              • 4. Re: Persistence.xml location
                I am also looking for the answer for this. If the location is different, how can I point to that location in the java code?
                • 5. Re: Persistence.xml location
                  Welcome to the forum. Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

                  I'm locking this thread now.