简体   繁体   中英

Set value of persistence.xml file with properties.config file

I want to set the values of my persistence.xml file with the values from my properties.config.

Is there any way to do this? Like any buildin function?

I think to a function like

factory.setvaluesfrompersistence(config.getpropertie("name"));

I want to do this bc I don't want to set my personal values in the persistence.xml so if I deploy this version there is no sesible data in there.

I use EclipseLink as JPA Propvider

my properties.config:

db.url=
db.user=
db.psw= 

and my persistens.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="test"
        transaction-type="RESOURCE_LOCAL">
        <class>my.test.test</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
                value="" />
            <property name="javax.persistence.jdbc.url"
                value="" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />

        </properties>

    </persistence-unit>
</persistence>

How I call it:

        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        final EntityManager em = factory.createEntityManager();

        final Query q = em.createQuery("select b from Beruf b");
        final List<Beruf> BerufeList = q.getResultList();
        for (final Beruf beruf : BerufeList) {
            System.out.println(beruf);
        }

        em.close();

Solution

Is to create a Map where you set the key from persistence.xml file and give it a new value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM