Skip to content

Instantly share code, notes, and snippets.

@rahulshivsharan
Last active December 10, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rahulshivsharan/4388343 to your computer and use it in GitHub Desktop.
Save rahulshivsharan/4388343 to your computer and use it in GitHub Desktop.
package sdf.common.sf;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public enum HibernateCon {
INSTANCE;
private SessionFactory sessionFactory;
private HibernateCon(){
System.out.println(" IN ENUM HibernateCon ");
sessionFactory = buildFactory();
}
private SessionFactory buildFactory(){
SessionFactory sf = null;
Configuration cfg = null;
ServiceRegistry serviceReg = null;
ServiceRegistryBuilder serviceRegBuilder = null;
Map<String, String> map = null;
try{
cfg = new Configuration();
map = new HashMap<String, String>();
map.put("hibernate.connection.driver_class", "org.apache.derby.jdbc.ClientDriver");
map.put("hibernate.connection.url", "jdbc:derby://localhost:1527/sun-appserv-samples");
map.put("hibernate.connection.username", "APP");
map.put("hibernate.connection.password", "app");
map.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
map.put("hibernate.show_sql", "true");
map.put("hibernate.format_sql", "true");
cfg.addResource("Student.hbm.xml");
serviceRegBuilder = new ServiceRegistryBuilder();
serviceReg = serviceRegBuilder.applySettings(map).buildServiceRegistry();
sf = cfg.buildSessionFactory(serviceReg);
}catch(Exception e){
System.out.println(" EXCEPTION WHILE BUILDING SESSION-FACTORY ");
e.printStackTrace();
}
return sf;
}
public SessionFactory getSessionFactory(){
return this.sessionFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment