0

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();
Jens
  • 67,715
  • 15
  • 98
  • 113
Phil
  • 35
  • 8
  • Are you using a framework like Spring or deploying your application in an application server? – Thomas Jan 24 '22 at 07:41
  • I use Maven in Eclipse. – Phil Jan 24 '22 at 07:54
  • So how are you bootstrapping EclipseLink then? Can you hook into that process, especially creation of the entity manager factory? If so, did you try passing the properties to the constructor/factory method? Besides that, did you try passing `javax.persistence.jdbc.user` directly from the command line? – Thomas Jan 24 '22 at 08:08
  • I am not sure what you want :/. I added EclipseLink over Maven in my Eclipse. And all I want is to pass the User and Password in my Code to the entity manager factory. Like i create a variable "db.user" = "test"; and then I just want to pass it to a func like factory.createEnityManager(db.user) or sth like this (beste case Build in). And it should "set" the values like if I write it in the persistence.xml. If I hard code it in my persiscene.xml it works like a charm and I get the values from my DB. – Phil Jan 24 '22 at 08:50
  • EclipseLink doesn't "automagically" start up if you have it as a dependency (at least not to my knowledge) so there needs to be some code that actually tells EclipseLink to start, create the factory etc. Where does `factory` in your code come from? – Thomas Jan 24 '22 at 09:04
  • "private static EntityManagerFactory factory;" in my class constructor – Phil Jan 24 '22 at 09:10
  • Well, and how is that field initialized? Where does the value come from? Please don't just post small snippets but provide a [mcve]. Read through the comments above and post relevant code. I'll try to recap them again: we need to see how you are initializing EclipseLink, where and how the factories are created etc. - if there's no such code in your application then you're likely using some framework and in that case we need to know which one. Without that information it's hard to help - this would amount to "it hurts" without telling us where. – Thomas Jan 24 '22 at 09:18
  • my Main calls : ` final BerufeEinlesenWorkflow bew = new BerufeEinlesenWorkflow(config); bew.run();` Config is my properties.config. and my class BerufeEinlesenWorkflow: – Phil Jan 24 '22 at 09:23
  • My class has the cunstructor: public class BerufeEinlesenWorkflow { private static final Logger logger = Logger.getLogger(BerufeEinlesenWorkflow.class); private static final String PERSISTENCE_UNIT_NAME = "berufe"; private static EntityManagerFactory factory; private final CSV_Reader myReader = new CSV_Reader(); private final PropertyManager config; public BerufeEinlesenWorkflow(PropertyManager config) { this.config = config; } – Phil Jan 24 '22 at 09:23
  • and there is the func run() with the code: public void run() throws IOException { final List berufe = myReader.fileRead(config.getProperty("input.file")); for (final Beruf b : berufe) { // System.out.println(b); } logger.info("Wrote CSV-file correct"); factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); final EntityManager em = factory.createEntityManager(); final Query q = em.createQuery("select b from Beruf b"); final List BerufeList = q.getResultList(); for (final Beruf beruf : BerufeList) { System.out.println(beruf); } em.close( – Phil Jan 24 '22 at 09:23
  • I'm not going to read that code in the comments as the formatting is horrible. Rather than providing additional information that's asked for in comments, go and [edit] your question and add it there (nicely formatted of course). You might also want to read [ask] for good measure. – Thomas Jan 24 '22 at 09:25
  • Finally, as I already suspected, you should be able to pass the properties when creating the persistence unit. Have a look at [`Persistence.createEntityManagerFactory(String,Map)`](https://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory-java.lang.String-java.util.Map-), read the properties you need from whatever location you want, put the values into the map and use keys as required by `persistence.xml` (the property names), and pass the map to that method. – Thomas Jan 24 '22 at 09:29
  • Works like a charm now. Big ty. – Phil Jan 24 '22 at 09:43

1 Answers1

0

Solution

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

Phil
  • 35
  • 8