Hibernate.cfg.xml - Set parameters from a properties file

I have this hibernate.cfg.xml file that I use to configure hibernate in sturtup.

<property name="hibernate.connection.username">dbUser</property>
<property name="hibernate.connection.password">1234</property>

I have a properties file, which he called config.properties , which contains all the other configurations used in the application.

How to set the "hibernate.connection.username" parameter from the properties file (config.properties), so that I only have one file to edit?

0
source share
3 answers

The solution for me is to add properties at runtime to the configuration:

configuration.setProperty("hibernate.connection.username", Config.db.getUser());
0
source

, config.properties

java, :

<property name="hibernate.connection.password">${propertyName}</property>

, , ...

0

:

Map<String, Object> prop = new HashMap<String, Object>();

prop.put("hibernate.connection.username", "asdf");
prop.put("hibernate.connection.password", "xxx");


Persistence.createEntityManagerFactory(persistenceUnitName, prop);
0

Source: https://habr.com/ru/post/1017034/


All Articles