Example usage for org.hibernate SessionFactory close

List of usage examples for org.hibernate SessionFactory close

Introduction

In this page you can find the example usage for org.hibernate SessionFactory close.

Prototype

void close() throws HibernateException;

Source Link

Document

Destroy this SessionFactory and release all resources (caches, connection pools, etc).

Usage

From source file:com.belajar.ORMHibernate.Main.MainHibernate.java

public static void main(String[] args) {
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    Person p = new Person();
    p.setName("Shinta");
    p.setPassword("diditshinta");
    System.out.print("saved!");

    Session session = sessionFactory.openSession();
    try {//from  ww  w .  j a v  a2s .  co m
        session.beginTransaction();
        session.save(p);
        session.getTransaction().commit();
    } catch (HibernateException hibernateException) {
        session.getTransaction().rollback();
    }
    session.close();

    session = sessionFactory.openSession();
    Query query = session.createQuery("from Person");
    List<Person> persons = query.list();
    for (Person person : persons) {
        System.out.println("Id: " + person.getId());
        System.out.println("Name: " + person.getName());
        System.out.println("Password: " + person.getPassword() + "\n");
    }

    session.close();
    sessionFactory.close();
}

From source file:com.cyclopsgroup.tornado.hibernate.impl.DefaultHibernateService.java

License:Open Source License

/**
 * Override or implement method of parent class or interface
 *
 * @see org.apache.avalon.framework.activity.Disposable#dispose()
 *//*from   ww w. j a  v  a 2  s .  c o m*/
public synchronized void dispose() {
    for (Iterator i = sessionFactories.values().iterator(); i.hasNext();) {
        SessionFactory sf = (SessionFactory) i.next();
        sf.close();
    }
    sessionFactories.clear();
}

From source file:com.db4o.drs.hibernate.impl.TablesCreatorImpl.java

License:Open Source License

/**
 * @throws RuntimeException when tables/columns not found
 *//*w ww. ja  v a  2  s.  c o m*/
public final void validateOrCreate() {
    SchemaValidator v = new SchemaValidator(_cfg);

    if (_cfg.getProperties().get(Environment.HBM2DDL_AUTO).equals("validate"))
        v.validate();
    else {
        SessionFactory sessionFactory = _cfg.buildSessionFactory();
        sessionFactory.close();
        v.validate();
    }
}

From source file:com.db4o.drs.hibernate.impl.Util.java

License:Open Source License

public static void initMySignature(Configuration cfg) {
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();//from w  ww  .j a v  a2 s . co  m
    Transaction tx = session.beginTransaction();

    if (session.createCriteria(MySignature.class).list().size() < 1)
        session.save(MySignature.generateSignature());

    tx.commit();
    session.close();
    sf.close();
}

From source file:com.db4o.drs.test.hibernate.HsqlMemoryFixture.java

License:Open Source License

public void clean() {
    if (config == null)
        return;/* ww w. ja v  a 2s  .  com*/

    SessionFactory sf = createConfig().configure(HSQL_CFG_XML).setProperty(Environment.URL, dbUrl)
            .buildSessionFactory();
    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();

    try {
        session.connection().createStatement().execute("SHUTDOWN IMMEDIATELY");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    tx.commit();
    session.close();
    sf.close();
}

From source file:com.duroty.utils.GeneralOperations.java

License:Open Source License

/**
 * DOCUMENT ME!/* w  w w. ja  v  a2 s  .c  om*/
 *
 * @param hfactory DOCUMENT ME!
 */
public static void closeHibernateSessionFactory(SessionFactory hfactory) {
    if (hfactory != null) {
        try {
            hfactory.close();
        } catch (HibernateException e) {
            DLog.log(DLog.DEBUG, GeneralOperations.class, e.getMessage());
        } finally {
            hfactory = null;
        }
    }
}

From source file:com.examples.hql.CriteriaQueriesExamples.java

public static void main(String[] args) {

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();//from   ww w .j a  v a  2 s . co m

    try {

        Criteria cr = session.createCriteria(PurchaseOrderHeader.class);

        Criterion c = Restrictions.ilike("ponumber", "PO%");

        cr.addOrder(Order.asc("orderValue"));

        cr.add(c);

        List<PurchaseOrderHeader> listPOH = cr.list();

        for (PurchaseOrderHeader poh : listPOH) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }
        System.out.println("----------***********-------------");

        session.close();

        sf.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.examples.hql.FirstLevelCacheExample.java

public static void main(String[] args) {

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();/*  ww  w.j av a  2 s . c o m*/
    //Transaction tx = session.beginTransaction();

    try {

        PurchaseOrderHeader po = (PurchaseOrderHeader) session.get(PurchaseOrderHeader.class, 2);

        System.out.println(po.getPoheaderId() + " | " + po.getPonumber() + " | " + po.getPodate() + " | "
                + po.getOrderValue());

        System.out.println("----------***********-------------");

        Session session1 = sf.openSession();

        PurchaseOrderHeader po1 = (PurchaseOrderHeader) session1.get(PurchaseOrderHeader.class, 2);

        System.out.println(po1.getPoheaderId() + " | " + po1.getPonumber() + " | " + po1.getPodate() + " | "
                + po1.getOrderValue());

        System.out.println("----------***********-------------");

        //            Query q=session.createQuery("update PurchaseOrderHeader set orderValue=:value where poheaderid=:id" );
        //            q.setParameter("value", new BigDecimal(1020124));
        //            q.setParameter("id", 2);
        //            
        //            q.executeUpdate();
        //            
        //            session.beginTransaction().commit();

        Session session2 = sf.openSession();

        PurchaseOrderHeader po2 = (PurchaseOrderHeader) session2.get(PurchaseOrderHeader.class, 2);

        System.out.println(po2.getPoheaderId() + " | " + po2.getPonumber() + " | " + po2.getPodate() + " | "
                + po2.getOrderValue());

        System.out.println("----------***********-------------");

        // tx.commit();
        session.evict(po);
        session1.evict(po1);
        session2.evict(po2);

        session.close();
        session1.close();
        session2.close();

        sf.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.examples.hql.HQLExamples.java

public static void main(String[] args) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();//from ww w.ja va  2 s.  c  o  m

    try {

        Query q = session.createQuery("from PurchaseOrderHeader");

        List<PurchaseOrderHeader> listPOH = q.list();

        for (PurchaseOrderHeader poh : listPOH) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        Query q1 = session.createQuery("from PurchaseOrderHeader where POHeaderID=:pohid");
        q1.setInteger("pohid", 1);

        PurchaseOrderHeader pohead = (PurchaseOrderHeader) q1.uniqueResult();

        System.out.println(pohead.getPoheaderId() + " | " + pohead.getPonumber() + " | " + pohead.getPodate()
                + " | " + pohead.getOrderValue());

        System.out.println("----------***********-------------");

        PurchaseOrderHeader po = (PurchaseOrderHeader) session.get(PurchaseOrderHeader.class, 2);

        System.out.println(po.getPoheaderId() + " | " + po.getPonumber() + " | " + po.getPodate() + " | "
                + po.getOrderValue());

        System.out.println("----------***********-------------");

        PurchaseOrderHeader po1 = (PurchaseOrderHeader) session.load(PurchaseOrderHeader.class, 3);

        System.out.println(po1.getPoheaderId() + " | " + po1.getPonumber() + " | " + po1.getPodate() + " | "
                + po1.getOrderValue());

        System.out.println("----------***********-------------");

        Query q2 = session.createQuery("from PurchaseOrderHeader where PONumber like '%PO%'");
        //q2.setParameter("PONumber", "PO102030");

        List<PurchaseOrderHeader> listPOH1 = q2.list();

        for (PurchaseOrderHeader poh : listPOH1) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        //Query q3=session.createQuery("select poh.ponumber,det.quantity from  PurchaseOrderHeader poh JOIN poh.purchaseOrderDetail det ");

        Query q3 = session.createQuery(
                "select poh.ponumber,det.quantity from  PurchaseOrderHeader poh LEFT JOIN poh.purchaseOrderDetail det ");

        List<Object[]> objList = q3.list();

        for (Object[] objList1 : objList) {

            System.out.println(objList1[0] + " | " + objList1[1]);
        }

        System.out.println("----------***********-------------");

        session.close();

        sf.close();

        System.out.println("Git by swamy....");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.examples.hql.NamedQueriesExamples.java

public static void main(String[] args) {

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();/*from w w  w. jav  a  2s .c  om*/

    try {

        Query q = session.getNamedQuery("HQL_GET_ALL_PURCHASEORDERS");

        List<PurchaseOrderHeader> listPOH = q.list();

        for (PurchaseOrderHeader poh : listPOH) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        Query q1 = session.getNamedQuery("HQL_GET_PURCHASEORDER_BY_ID");
        q1.setParameter("id", 1);
        PurchaseOrderHeader poh = (PurchaseOrderHeader) q1.uniqueResult();

        System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                + poh.getOrderValue());

        System.out.println("----------***********-------------");

        Query q2 = session.getNamedQuery("HQL_GET_PURCHASEORDER_BY_PONUMBER");
        q2.setParameter("ponumber", "PO102030");
        PurchaseOrderHeader poh1 = (PurchaseOrderHeader) q2.uniqueResult();

        System.out.println(poh1.getPoheaderId() + " | " + poh1.getPonumber() + " | " + poh1.getPodate() + " | "
                + poh1.getOrderValue());

        System.out.println("----------***********-------------");

        session.close();
        sf.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}