How to change persistence.xml file settings through an external file?

Asked

Viewed 1,232 times

0

I wanted to know how to change the configuration of the persistence.xml file through an external file. I am using JSF + JPA. I want to do this so as not to leave this setting stuck in the source code.

Somebody give me a hand?

  • Daniel, what kind of configuration of your persistence.xml can vary? Could you give an example?

  • Like change the database url, user and password, change update to create, that sort of thing. Ex: <Property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/filename" /> change this setting for example

  • You’re using Tomcat 7, 8?

  • I’m using Tomcat 7

2 answers

1

I believe that the best solution is to inform your connection settings in Tomcat, not in the project. Following documentation of Tomcat 7:

Within the Context of your application, create a Resource (attention to the name: jdbc/TestDB):

<Context>
    <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>
</Context>

In his web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
    <description>MySQL Test App</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

In his persistence.xml:

<persistence-unit name="TestUnit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/TestDB</jta-data-source>
    <!-- Demais configurações pertinentes -->
</persistence-unit>

This way, your project will always be the same. What will change is the configuration of your server/container.

  • Type I need to install the database on a different computer. In this case I need to configure the connection. This configuration allows this configuration?

  • 1

    Exactly. Imagine that your project .jar, .war, or whatever, it will always be the same, but in each customer - customer in the commercial scope - the database is in a different location, which, from made these settings, you specify in Tomcat itself (the customer’s).

  • I found this option interesting. But I could not find something to help me make this configuration in Toncat.

1

The method Persistence.createEntityManagerFactory has an Overload that you can pass a Map with persistence.xml settings, then you load them from where you think best (eg a configuration file);

Map properties = new HashMap();

properties.put("javax.persistence.jdbc.driver", "oracle.jdbc.OracleDriver");
properties.put("javax.persistence.jdbc.url", "jdbc:oracle:thin:@localhost:1521:ORCL");
properties.put("javax.persistence.jdbc.user", "user-name");
properties.put("javax.persistence.jdbc.password", "password");

Persistence.createEntityManagerFactory("unit-name", properties);
  • But where would I create this map, in what class? I didn’t understand very well.. And where I indicate the location of the file?

  • You don’t call the Persistence method.createEntityManagerFactory to create the Factory? It may be that you are using some api that already does this for you, then my solution will not work.

  • Yes. I do it in a specific class.

  • About the file location, I use an environment variable to indicate the path.

  • In this class that you call Persistence.createEntityManagerFactory("name-da-Unit'), you load the map information from the file (I use the java Properties class for this), and call Persistence.createEntityManagerFactory("name-da-Unit", mapComAsConfigurations);

  • Actually I am wanting to use a file . properties to perform the configuration of persistece.xml. However I am not able to read the file

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.