package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static String username = "";
private static String password = "";
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if(sessionFactory == null){
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration config = new Configuration().configure();
config.setProperty("hibernate.connection.username", username);
config.setProperty("hibernate.connection.password", password);
sessionFactory = config.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);
}
}
return sessionFactory;
}
public static void initialize(String username, String password){
HibernateUtil.username = username;
HibernateUtil.password = password;
}
}