Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistence.xml field values from properties file

Help, i would like to my persistence xml properties value reference to my db.properties file.

here's my db.properties file

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/apsas
jdbc.username=root
jdbc.password=password

and here's my current persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/apsas" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

what i wanted to do is to set its property into like this

<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="${jdbc.driver}" />
            <property name="hibernate.connection.url" value="${jdbc.url}" />
            <property name="hibernate.connection.username" value="${jdbc.username}" />
            <property name="hibernate.connection.password" value="${jdbc.password}" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

question is how can i import the properties file into my persistence xml Advance thanks for those who will help me...

like image 474
Ernest Hilvano Avatar asked Dec 10 '13 04:12

Ernest Hilvano


1 Answers

If you are using maven, you can use source filtering for this. Use this docs for reference. I will outline the process a bit.

You will need to specify the filtering path. I am not sure if you have to explicitly define the included files.

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/resources/META-INF</directory>
      <filtering>true</filtering>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
    ...
  </resources>
  ...
</build>

The directory you define is relative to the pom.xml.

You can now define regular maven properties to replace the placeholders in your persistence.xml

If you want to have the properties in a separate .properties file, you need to tell maven, where to find that file:

<filters>
  <filter>db.properties</filter>
</filters>

Filtering occurs on mvn resources:resources. This step is defined for all the packaging goals you will execute on deployment.

You can use maven profiles to switch between sets of properties or .properties files.

like image 134
kostja Avatar answered Sep 21 '22 16:09

kostja