Skip to content

Instantly share code, notes, and snippets.

@rahulshivsharan
Last active December 10, 2015 05:29
Show Gist options
  • Select an option

  • Save rahulshivsharan/4388343 to your computer and use it in GitHub Desktop.

Select an option

Save rahulshivsharan/4388343 to your computer and use it in GitHub Desktop.

Revisions

  1. rahulshivsharan renamed this gist Dec 27, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rahulshivsharan created this gist Dec 27, 2012.
    51 changes: 51 additions & 0 deletions HibernateCon
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    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;
    }
    }