Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate configuration in both file and code

Tags:

java

hibernate

Sorry if this seems to be duplicate - but the answers on the other posts did not help. I am developing an application where the database detail is stored somewhere else and I have to read it from this standard location. I have the files hibernate.properties and hibernate.cfg.xml in my program root, and I also have the following configuration which is set programmaticaly (the values are hard-coded until I get it to work...):

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 the other settings such as dialect are stored in the hibernate.properties file.)

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

However, the behaviour that I get is the following: If I put ALL the config in hibernate.properties, it works perfectly (i.e. hibernate picks up the properties file). If I only put the username and password in code, it fails with "invalid user/pass". It is as if the properties that I set programmatically are always ignored. You can probably guess that if I put the detail in both places, the settings in the config file is used.

What I would like is the settings that I set programmatically MUST override any settings in the properties file.

Any ideas?


Update: I added a user/pass in both places. The file's was called "set-in-file" and the code's was called "set-in-code". I then added a System.out.println() just before creating the SessionFactory... and I got this (partial snip):

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 code are processed... however, for some reason ignored...

like image 536
Jaco Van Niekerk Avatar asked May 17 '12 07:05

Jaco Van Niekerk


1 Answers

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

I replaced the code with:

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.

like image 140
Jaco Van Niekerk Avatar answered Oct 22 '22 03:10

Jaco Van Niekerk