Not entirely sure what you mean. I use a HibernateUtil class in my eclipse project. You can either get the util class to read a hibernate config file or configure it programatically:
Reading a config file thats located in the x.x.x.storage.hibernate package:
HibernateUtil.java
Code:
package com.company.project.storage.hibernate;
public class HibernateUtil {
public static final Configuration cfg = new Configuration();
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
cfg.configure("/com/company/project/storage/hibernate/hibernate.cfg.xml");
sessionFactory = cfg.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Configuring the session factory programatically - no config file necessary. This is a very simplistic implementation that I use for testing. I have to manually manage my session open/close rather than having a currentSession bound to the thread. Its useful for testing because you can explicitly get a brand new session and so ensure level1 caches are empty.
HibernateUtil.java
Code:
package test.groupbydate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static {
Configuration config = new Configuration()
.addClass(Item.class)
// HSQLDB Config
// .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
// .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
// .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
// .setProperty("hibernate.connection.username", "sa")
// .setProperty("hibernate.connection.password", "")
// .setProperty("hibernate.hbm2ddl.auto", "create-drop")
// MYSQL Config
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "password")
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
.setProperty("hibernate.show_sql", "true");
HibernateUtil.setSessionFactory(config.buildSessionFactory());
}
public static synchronized Session getSession() {
if (factory == null) {
factory = new Configuration().configure().buildSessionFactory();
}
return factory.openSession();
}
public static void setSessionFactory(SessionFactory factory) {
HibernateUtil.factory = factory;
}
}