Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

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

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:uk.ac.sussex.model.base.BaseFactory.java

License:Open Source License

public BaseObject fetchSingleSubclassObjectByRestrictions(BaseObject subclassObject,
        RestrictionList restrictions) throws Exception {
    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    BaseObject object = null;//from www .ja  va  2s.c o m
    try {
        session.getTransaction().begin();
        Criteria crit = session.createCriteria(subclassObject.getClass());
        for (Criterion c : restrictions) {
            crit.add(c);
        }
        object = (BaseObject) crit.uniqueResult();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return object;
}

From source file:uk.ac.sussex.model.base.BaseObject.java

License:Open Source License

public void save() throws Exception {
    this.validateWith(new GenericAttributeValidator());

    SessionFactory sf = SessionFactoryHelper.getSessionFactory();

    Session session = sf.getCurrentSession();

    try {/* w ww  .j a v  a  2s  . c  o  m*/
        session.getTransaction().begin();

        session.saveOrUpdate(this);

        session.getTransaction().commit();

    } catch (Exception e) {
        session.getTransaction().rollback();
        e.printStackTrace();
        String errorMessage = e.getMessage();
        String classname = this.getClass().getName();
        if (errorMessage.contains("same identifier")
                || errorMessage.contains("Could not execute JDBC batch update")) {
            throw new Exception("Error saving " + classname + ": Duplicate identifier" + errorMessage);
        } else {
            throw new Exception(errorMessage);
        }
    }
}

From source file:uk.ac.sussex.model.base.BaseObject.java

License:Open Source License

public void delete() throws Exception {
    SessionFactory sf = SessionFactoryHelper.getSessionFactory();

    Session session = sf.getCurrentSession();

    try {/*from w  w  w .j  ava 2  s  .c  o m*/
        session.getTransaction().begin();

        session.delete(this);

        session.getTransaction().commit();

    } catch (Exception e) {
        session.getTransaction().rollback();
        String errorMessage = e.getMessage();
        throw new Exception(errorMessage);
    }
}

From source file:uk.ac.sussex.model.BillFactory.java

License:Open Source License

public Integer countHouseholdUnpaidBills(Hearth hearth) throws Exception {
    Integer pendingBillsCount = 0;
    String query = "select count(*) from Bill b " + "where b.payee = " + hearth.getId().toString()
            + " and b.paid != " + Bill.BEEN_PAID;

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {/*from  w w  w. j av  a  2s .  c  o  m*/
        session.getTransaction().begin();
        pendingBillsCount = ((Number) session.createQuery(query).iterate().next()).intValue();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return pendingBillsCount;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public Task getHouseholdTask(Integer taskId) throws Exception {
    Task object = null;// ww w  .j ava  2 s .  c om
    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    try {
        session.getTransaction().begin();

        object = (Task) session.get(Task.class, taskId);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        session.getTransaction().rollback();
        throw (new Exception("hibernate problem! " + e.getMessage()));
    }
    if (object == null) {
        throw (new Exception("No task with that identifier: " + taskId));
    }
    return object;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public Integer countPendingLocationTasks(Hearth household, Location loc, String taskType) throws Exception {
    Integer pendingTaskCount = 0;
    String query = "select count(*) from Task t " + "where t.household = " + household.getId().toString()
            + " and t.location = " + loc.getId().toString() + " and t.status = " + Task.PENDING
            + " and t.deleted = 0 " + " and t.class = '" + taskType + "'";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {/*from w ww . jav a  2 s. c o  m*/
        session.getTransaction().begin();
        pendingTaskCount = ((Number) session.createQuery(query).iterate().next()).intValue();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return pendingTaskCount;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public Integer countPendingLocationTasks(Hearth household, Location loc) throws Exception {
    Integer pendingTaskCount = 0;
    String query = "select count(*) from Task t " + "where t.household = " + household.getId().toString()
            + " and t.location = " + loc.getId().toString() + " and t.status = " + Task.PENDING
            + " and t.deleted = 0";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {/*  w  ww.  j  a  v  a  2 s  .  co  m*/
        session.getTransaction().begin();
        pendingTaskCount = ((Number) session.createQuery(query).iterate().next()).intValue();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return pendingTaskCount;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public Integer countPendingActorTasks(Hearth household, AllChars actor) throws Exception {
    Integer pendingTaskCount = 0;
    String query = "select count(*) from Task t " + "where t.household = " + household.getId().toString()
            + " and t.actor = " + actor.getId().toString() + " and t.status = " + Task.PENDING
            + " and t.deleted = 0";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {//from   ww  w  .  j av  a 2 s. co m
        session.getTransaction().begin();
        pendingTaskCount = ((Number) session.createQuery(query).iterate().next()).intValue();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return pendingTaskCount;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public List<Task> fetchPendingTasks(Hearth household, String taskType) throws Exception {
    List<Task> pendingTasks = null;
    String query = "select t from Task t " + "where t.household = " + household.getId().toString()
            + " and t.status = " + Task.PENDING + " and t.class = '" + taskType + "'";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();//  w w w. j  a va2 s  .  co m
    @SuppressWarnings("rawtypes")
    Iterator results = null;
    try {
        results = session.createQuery(query).iterate();
    } catch (Exception e) {
        session.getTransaction().commit();
        throw e;
    }
    if (results != null) {
        pendingTasks = new ArrayList<Task>();
        while (results.hasNext()) {
            pendingTasks.add((Task) results.next());
        }
    }
    session.getTransaction().commit();
    return pendingTasks;
}

From source file:uk.ac.sussex.model.tasks.TaskList.java

License:Open Source License

public int countAllPendingTasks(Hearth household) throws Exception {
    String query = "select count(*) from Task t " + "where t.household = " + household.getId().toString()
            + " and t.status = " + Task.PENDING + " and t.deleted = 0";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    int pendingTaskCount;
    try {/*from w w w . j a v  a2  s. c  om*/
        session.getTransaction().begin();
        pendingTaskCount = ((Number) session.createQuery(query).iterate().next()).intValue();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return pendingTaskCount;
}