Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persistence.xml to import database parameters values from .properties file

Edit: not duplicate but almost

I would like to have my app persistence.xml to be something like

<?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_1_0.xsd"
                version="1.0">
   <persistence-unit name="appName" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="${db.dialect'}"/>
            <property name="javax.persistence.jdbc.driver" value="${db.driver}"/>
            <property name="javax.persistence.jdbc.user" value="${db.user}"/>
            <property name="javax.persistence.jdbc.password" value="${db.password}"/>
            <property name="javax.persistence.jdbc.url" value="${db.url}"/>
        </properties>
    </persistence-unit>
</persistence>

getting these placeholder values from a simple text file somewhere in my source folders.

I read about that it's possible when using Spring doing like

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>

but here we are not using Spring, just Hibernate and some Primefaces.

Is it possible?

Thanks!

Edit: I didn't mention some things, but for reference, I'm also using Shiro Security and Ant to do some stuff. I'll post the solution as an answer. This makes my project have 3 different files with database parameters:

  • persistence.xml (Hibernate)
  • context.xml (Shiro)
  • database.properties (for Flyway tasks in Ant)
like image 406
paulochf Avatar asked Oct 03 '13 23:10

paulochf


People also ask

How do you specify data source in persistence xml?

xml , choose the Source tab. Specify the data source for the persistence unit. Typically, you use the relevant data source alias name to specify the DataSource. Make sure that the value of the jta-data-source tag is the same as the value of the alias tag in the data-source-aliases.

What is the purpose of the persistence xml file?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).

What is the role of the persistence xml file in JPA?

The persistence. xml file gives you complete flexibility to configure the EntityManager. The persistence. xml file is a standard configuration file in JPA.

What is JTA persistence xml?

xml file. This file defines a persistence unit named OrderManagement, which uses a JTA-aware data source jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses.


2 Answers

Instead of defining the properties inside persistence.xml you can define them in a standard properties file (key=value) and pass a Properties object to the createEntityManagerFactory() method, e.g.:

Properties props = new Properties();
props.load(new FileInputStream("/some/path/persistence.properties"));
EntityManagerFactory factory = Persistence.createEntityManagerFactory("appName", props);
like image 83
David Levesque Avatar answered Nov 12 '22 19:11

David Levesque


If you are using Maven as the build system, you can use Maven filters to replace the values during build time.

Or you can write a simple property placeholder replacement (which is internally used by spring itself)

Reference: https://stackoverflow.com/a/14724719/477435

like image 44
Gireesh Avatar answered Nov 12 '22 18:11

Gireesh