1

I've this file hibernate.cfg.xml that I use to configure hibernate at sturtup.

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

I've a properties file that it's called config.properties that hold all other configurations used into the application.

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

6
  • Are you searching for something like this ? Commented Mar 18, 2015 at 14:17
  • @NeerajJain that works when using an IoC and dependency injection framework like Spring (as shown in that link). In case you don't use it, you can do it with a little more effort. Commented Mar 18, 2015 at 14:18
  • there is no way to set parameters from Java code during hibernate startup? Commented Mar 18, 2015 at 14:25
  • AFAIK the best way is to create a DataSource using Apache DBCP, C3P0, HikariCP or another vendor, and then follow the steps shown here: stackoverflow.com/q/4406935/1065197. Note that when you create your DataSource instance, you can apply the parameters from a properties file as you want/need. Commented Mar 18, 2015 at 14:27
  • @LuiggiMendoza I've found that which work for me: configuration.setProperty( "hibernate.connection.username", "1234" ); What do you think about? Commented Mar 18, 2015 at 14:35

3 Answers 3

0

It depends on the destination of the properties stored in config.properties

If they go in the java System properties, you can use them in your hibernate config like this :

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

If your properties go somewhere else, then I don't think hibernate will see them naturally...

Sign up to request clarification or add additional context in comments.

1 Comment

Yes is used somewhere else. How can I allow Hibernate to see this properties?
0

The solution for me is that, Adding a properties at runtime into the configuration:

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

Comments

0

I do it like that:

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

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


Persistence.createEntityManagerFactory(persistenceUnitName, prop);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.