Example usage for org.hibernate SessionFactory openSession

List of usage examples for org.hibernate SessionFactory openSession

Introduction

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

Prototype

Session openSession() throws HibernateException;

Source Link

Document

Open a Session .

Usage

From source file:agenda_hibernate.controlador.Operaciones.java

public DefaultListModel obtenerNoticiasUser(String user) {
    SessionFactory sesion = NewHibernateUtil.getSessionFactory();
    Session session = sesion.openSession();
    Transaction tx = session.beginTransaction();
    Query q = session.createQuery("from noticias where noticias.usuario=" + user + "");
    List<Noticias> lista = q.list();
    Iterator<Noticias> iter = lista.iterator();
    tx.commit();/* w w  w  . jav  a  2  s .  co m*/
    session.close();
    DefaultListModel dlm = new DefaultListModel();
    while (iter.hasNext()) {
        Noticias noti = (Noticias) iter.next();

        dlm.addElement(noti);
    }
    return dlm;
}

From source file:agenda_hibernate.controlador.Operaciones.java

public DefaultListModel mostrarNoticias() {
    SessionFactory sesion = NewHibernateUtil.getSessionFactory();
    Session session = sesion.openSession();
    Transaction tx = session.beginTransaction();
    Query q = session.createQuery("from Noticias");
    List<Noticias> lista = q.list();
    Iterator<Noticias> iter = lista.iterator();
    tx.commit();//from  w ww. j  a  v a2s . co  m
    session.close();
    DefaultListModel dlm = new DefaultListModel();
    while (iter.hasNext()) {
        Noticias noti = (Noticias) iter.next();
        dlm.addElement(noti);
    }
    return dlm;
}

From source file:alpha.portal.dao.hibernate.PayloadDaoHibernate.java

License:Apache License

/**
 * Internal function to load the highest sequenceNumber from the AlphaCard
 * table./*from   w w w.  j  ava  2  s . co  m*/
 * 
 * @param sessionFactory
 *            the session factory
 * @param column
 *            the column
 * @return 0 if no record is found
 */
private Long getLastValue(final SessionFactory sessionFactory, final String column) {
    Session session;
    boolean sessionOwn = false;
    try {
        session = sessionFactory.getCurrentSession();
    } catch (final Exception e) {
        session = sessionFactory.openSession();
        sessionOwn = true;
    }
    final Query q = session.createSQLQuery("select max(" + column + ") from payload");
    final List<Object> list = q.list();
    BigInteger value = (BigInteger) list.get(0);
    if (value == null) {
        value = new BigInteger("0");
    }
    if (sessionOwn) {
        session.close();
    }
    return value.longValue();
}

From source file:Api.GetTest.java

/**
 * Retrieves representation of an instance of Api.GetTest
 *
 * @return an instance of java.lang.String
 *///ww w  .  j a  v a 2  s  .co  m
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJson() {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session s = sessionFactory.openSession();
    Criteria cr = s.createCriteria(FirstSubcategory.class);
    List results = cr.list();
    JSONSerializer serializer = new JSONSerializer();
    sessionFactory.close();
    return serializer.exclude("*.class").serialize(results);
}

From source file:app.core.Db.java

License:Open Source License

public static Session getSession() {
    SessionFactory sessionFactory = getSessionFactory();
    Assert.state(sessionFactory != null, "SessionFactory not injected");
    // Spring TransactionSynchronizationManager keeps the current session available for us via TransactionSynchronizationManager.getResource("sessionHolder").
    //        Assert.isTrue(TransactionSynchronizationManager.isActualTransactionActive(), "No need to go any further if no transaction.");
    try {//www.ja v a2s.  c om
        return sessionFactory.getCurrentSession();
    } catch (HibernateException e) {
        return sessionFactory.openSession();
    }
    //        return (Session) em().getDelegate();
}

From source file:app.Ejer1.java

public static void insertSeguro(Seguro seguro) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();//from  w w w . j av a2  s . com
    session.save(seguro);

    session.getTransaction().commit();
    session.close();

}

From source file:app.Ejer1.java

public static Seguro readSeguro(int clavePrimaria) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    Seguro seguro = (Seguro) session.get(Seguro.class, clavePrimaria);

    System.out.println(seguro.getNombre());

    session.close();//from   ww  w  .  j  av a2  s. c  om

    return seguro;

}

From source file:app.Ejer1.java

public static void updateSeguro(Seguro seguro) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    seguro.setNombre("Edgar Modificado");

    session.beginTransaction();/*from w ww .ja  v  a2s  . co  m*/
    session.update(seguro);
    session.getTransaction().commit();

    session.close();
}

From source file:app.Ejer1.java

public static void deleteSeguro(Seguro seguro) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    session.beginTransaction();//  w  w  w . j  ava  2s  .  co m
    session.delete(seguro);
    session.getTransaction().commit();

    session.close();

}

From source file:app.Ejer2.java

public static void insertSeguro(SeguroAnotaciones seguro) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();// w  w w . j a va2  s.co m
    session.save(seguro);

    session.getTransaction().commit();
    session.close();

}