Hibernate configuration in both file and code

Sorry if this seems like a duplicate, but replies to other posts did not help. I am developing an application where database details are stored somewhere else, and I have to read it from this standard location. I have the hibernate.properties and hibernate.cfg.xml files in the root of my program, and I also have the following configuration that is set programmatically (the values ​​are hard-coded until I get it working ...):

Configuration configuration = new Configuration().configure(); configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.pOracleDriver"); configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@dte_datalayer:1521:DTE"); configuration.setProperty("hibernate.connection.username", "testuser"); configuration.setProperty("hibernate.connection.password", "testpass"); SessionFactory sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry()); 

(Note that other settings, such as dialect, are stored in the hibernate.properties file.)

As I understand it, configure () will correctly load default properties / mappings? My "setProperty ()" statements override the default settings, right?

However, the behavior I get is the following: if I put ALL the configuration in hibernate.properties, it works fine (i.e., hibernate selects the properties file). If I just put the username and password in the code, it fails with "invalid user / password". As if the properties that I set programmatically are always ignored. You can probably guess that if I put the parts in both places, the settings in the configuration file will be used.

I would like the settings that I set programmatically to SHOULD override any parameters in the properties file.

Any ideas?


Update: I added a user / password in both places. The file was called set-in-file, and the code was called set-in-code. Then I added System.out.println () just before creating the SessionFactory ... and got this (partial snippet):

java.runtime.name = Java (TM) SE Runtime Environment, hibernate.connection.password = set-in-code, hibernate.cache.provider_class = org.hibernate.cache.NoCacheProvider, sun.boot.library.path = / usr /lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64, java.vm.version = 20.1-b02, hibernate.connection.username = testuser, ....

This means that the settings in the code are being processed ... however, for some reason, they are ignored ...

+6
source share
1 answer

Okay .. I was confused by the old and new versions of Hibernate ... the problem is in the last line.

I replaced the code:

 Configuration configuration = new Configuration(); configuration.configure(); configuration.setProperty("hibernate.connection.username", "whomever"); configuration.setProperty("hibernate.connection.password", "whatever"); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); ... 

... and it works.

+6
source

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


All Articles