0

We provide db credential in hibernate.cfg.xml as

<hibernate-configuration>
    <session-factory>
       <property name="hibernate.connection.url">url</property>
       <property name="hibernate.connection.username">username</property>
       <property name="hibernate.connection.password">password</property>
    <session-factory>
<hibernate-configuration>

Either we can provide these properties here or in hibernate.properties in classpath. But I want them to come from an external file. I couldn't find a way in hibernate to change the path of default hibernate.properties file.

Please help.

[EDIT] The method in java which generates sessionFactory object

public class HibernateUtil {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    // Create the session factory from hibernate.cfg.xml
    Configuration configuration = new Configuration();
    StandardServiceRegistryBuilder serviceRegistryBuilder =
        new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    return configuration.buildSessionFactory(serviceRegistryBuilder.build());
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}
1
  • How are you invoking the sessionFactory in your Java code.? Commented Jul 30, 2014 at 16:39

1 Answer 1

3

Programmatically, you can load XML and properties like this:

public class MyHibernate {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        URL r1 = MyHibernate.class.getResource("/hibernate.cfg.xml");
        Configuration c = new Configuration().configure(r1);

        try {
            InputStream is = MyHibernate.class.getResourceAsStream("/hibernate.properties");
            Properties props = new Properties();
            props.load(is);
            c.addProperties(props);
        } catch (Exception e) {
            LOG.error("Error reading properties", e);
        }

        return c.buildSessionFactory();
    } catch (Throwable ex) {
        LOG.error("Error creating SessionFactory", ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

Through Spring

You can use a PropertiesFactoryBean to read-in the properties from your file and configure your LocalSessionFactoryBean:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="hibernateProperties">
      <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">path-to-properties-file</property>
      </bean>
    </property>
    ...
  </bean>

Hope it be useful.

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

1 Comment

I didn't want to use InputStream class to load hibernate properties, is there a way to include this in spring

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.