0

I have persistence.xml file which is used to load database and connections.

I have the username and password stored in it like this:

            <!-- MySQL -->
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/mydatabase"/>

            <!-- Credentials -->
            <property name="javax.persistence.jdbc.user"
                      value="this is user"/>
            <property name="javax.persistence.jdbc.password"
                      value="this is password"/>

Now I have a requirement to not store these like this in codebase and want to fetch them programatically through a wire. How can I achieve that ?

1 Answers1

0

to set the values dynamically, you can do it like this:

in config.properties set these things and then access them

jdbc.url=jdbc:mysql://localhost:3306/my_database
jdbc.username=root
jdbc.password=123

<property name="javax.persistence.jdbc.url" value="${jdbc.url}" />
<property name="javax.persistence.jdbc.password" value="${jdbc.properties}" />

or if you want to fetch them in the controller, use can fetch them by using "EntityManagerFactory"

EntityManagerFactory emf = entityManager.getEntityManagerFactory();
Map<String, Object> emfProperties = emf.getProperties();

String url = (String) emfProperties.get("javax.persistence.jdbc.url");
String user = (String) emfProperties.get("javax.persistence.jdbc.user");
String password = (String) emfProperties.get("javax.persistence.jdbc.password");
Mani
  • 267
  • 2
  • 11