This discussion is archived
10 Replies Latest reply: Jan 28, 2013 11:02 PM by 689442 RSS

How to get properties

689442 Newbie
Currently Being Moderated
application.properties file contains following information:
-----------------application.properties--------------------
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
jdbc.user=username
jdbc.password=...

However, I have a hibernate.cfg.xml file which has
<property name="connection.url">
               jdbc:oracle:thin:@localhost:1521:orcl
          </property>
          <property name="connection.username">username</property>
          <property name="connection.password">...</property>

How to get database connection information from application.properties and set to hibernate.cfg.xml?

Thanks.
  • 1. Re: How to get properties
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    How to get database connection information from application.properties and set to hibernate.cfg.xml?
    First, you seem to have the same information in both.

    Secondly, if you didn't you would open a text editor and change the information in one of them.
  • 2. Re: How to get properties
    jwenting Journeyer
    Currently Being Moderated
    runtime? You don't do that.
    You can create a Hibernate context at runtime and set the values in that that you either read from the properties file or request from the application server, but you don't at runtime write to the Hibernate config file (unless you're creating an editor for those files, in which case you'd have it in memory as a DOM object and just use the built-in functionality of the Java XML APIs to read, write, and modify the DOM.
  • 3. Re: How to get properties
    689442 Newbie
    Currently Being Moderated
    I have same database connection information in 2 files (application.properties and hibernate.cfg.xml). Both files are needed.
    I will not change these files at runtime.
    But I want the database connection information in application.properties. And hibernate.cfg.xml has mapping information and read database connection information from application.properties.
    How to do that?

    Thanks.
  • 4. Re: How to get properties
    Jörg Explorer
    Currently Being Moderated
    read database connection information from application.properties.
    Have a look at the Properties class
    or go to the tutorial
  • 5. Re: How to get properties
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    But I want the database connection information in application.properties. And hibernate.cfg.xml has mapping information and read database connection information from application.properties.
    How to do that?
    So you want to avoid having the connection parameters in 2 places?
    Get rid of the application properties and make a datasource instead.
  • 6. Re: How to get properties
    689442 Newbie
    Currently Being Moderated
    Yes, I want to avoid same database connection information in 2 different files.

    Do you mean in the applicationContext.xml, I should change the contexts as follow?

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
    <property name="jdbcUrl" value="jdbc\:oracle\:thin\:@localhost\:1521\:orcl" />
    <property name="user" value="username" />
    <property name="password" value="password" />
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="20"/>
    <property name="initialPoolSize" value="1"/>
    <property name="maxIdleTime" value="25000"/>
    <property name="acquireIncrement" value="1"/>
    ...........................

    However, if I remove database connection information from hibernate.cfg.xml and only contain mapping information, the web services of this project cannot run but other parts of this application is fine.
  • 7. Re: How to get properties
    r035198x Pro
    Currently Being Moderated
    1.)You really should be asking all your spring questions in a spring forum
    2.) Property files on the classpath for environment variables is not a good idea because you have to redeploy the application to change those.
    3.) If you are going to deploy to some container e.g tomcat then configure a datasource (as was suggested) and let your applications use the connection settings via the configured jndi resource then point your hibernate .cfg.xml to the jndi resource using the property
    <property name="hibernate.connection.datasource">yourContainerConfiguredJNDIName</property>
  • 8. Re: How to get properties
    689442 Newbie
    Currently Being Moderated
    If I use:
    import org.hibernate.SessionFactory;  
    import org.hibernate.cfg.AnnotationConfiguration;
    
    public class HibernateUtil {
    
        private static  SessionFactory sessionFactory = buildSessionFactory();
        private static SessionFactory buildSessionFactory() {
            try {
                // Create the SessionFactory from hibernate.cfg.xml
    
                 return new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
            
            }
            catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
     
    does it mean that I must use hibernate.cfg.xml and database connection information must be in this file?
  • 9. Re: How to get properties
    r035198x Pro
    Currently Being Moderated
    Yes you can use the hibernate.cfg.xml file but it doesn't have to contain the connection details directly. It can point to a container configured datasource with the connection details (like I already said above). Don't put the connection details in there if they are to be used outside of hibernate otherwise you need to repeat them again.
  • 10. Re: How to get properties
    689442 Newbie
    Currently Being Moderated
    Thank you!
    After adding <property name="hibernate.connection.datasource">jndiname</property> and remove database connection detail in hibernate.cfg.xml, it works.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points