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

    Persistence.xml location

    843833
      Hi.
      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?

      Regards,
      Andy Morton
        • 1. Re: Persistence.xml location
          843833
          is this not possible?
          • 2. Re: Persistence.xml location
            843833
            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:
               
            <jta-data-source>jdbc/OURJPADATASOURCE</jta-data-source>
            <mapping-file>META-INF/orm-live.xml</mapping-file>
            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"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                   xmlns:util="http://www.springframework.org/schema/util"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   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">
            
                 <context:annotation-config/>
                 <tx:annotation-driven  transaction-manager="h8TransactionManager"/> 
                 
                 <alias alias="dataSource" name="hsqlDataSource"/>
                 <bean id="hsqlDataSource"
                       class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                      <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>
                 
                 <bean id="h8JPAVendorAdapter"
                       class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                      <property name="showSql"           value="false"/>
                      <property name="generateDdl"           value="true"/>
                      <property name="database"           value="HSQL"/>
                 </bean>
            
                 <alias alias="entityManagerFactory" name="h8EntityManagerFactory"/>
                 <bean id="h8EntityManagerFactory" 
                       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                     <property name="dataSource"              ref="dataSource"/>
                     <property name="jpaVendorAdapter"        ref="h8JPAVendorAdapter"/>
                     <property name="persistenceXmlLocation"  value="classpath:/test/jpa/persistence.xml"/>
                     <property name="persistenceUnitName"     value="PUNAME"/>
                 </bean>
                 
                 <alias alias="transactionManager" name="h8TransactionManager"/>
                 <bean id="h8TransactionManager" 
                       class="org.springframework.orm.jpa.JpaTransactionManager">
                      <property name="entityManagerFactory"      ref="h8EntityManagerFactory"/>
                      <property name="dataSource"           ref="dataSource"/>
                 </bean>
            
                 <bean id="jpaType" class="biz.wss.jpa.JPAType" factory-method="valueOf">     
                     <constructor-arg value="HIBERNATE"/>
                 </bean>
            Hope that helps a bit...
            Ed

            Edited by: edrandall on Mar 20, 2009 9:24 AM
            • 3. Re: Persistence.xml location
              843833
              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
                843833
                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
                  PhHein
                  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.