title: Hibernate configuration in both file and code
body: <p>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...):</p>
  
  <pre><code>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());
  </code></pre>
  
  <p>(Note that the other settings such as dialect are stored in the hibernate.properties file.)</p>
  
  <p>As I understand it, configure() will load the default properties/mappings right? My "setProperty()" statements will override the standard settings, right?</p>
  
  <p>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.</p>
  
  <p>What I would like is the settings that I set programmatically MUST override any settings in the properties file.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p>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):</p>
  
  <p>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, ....</p>
  
  <p>This means that the settings in code are processed... however, for some reason ignored...</p>