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.tasks.TaskList.java

License:Open Source License

public List<Task> fetchCurrentExternalTasks(Location loc, String taskType) throws Exception {
    SeasonDetail sd = this.game.getCurrentSeasonDetail();
    List<Task> pendingTasks = null;
    String query = "select t from Task t " + "where t.location = " + loc.getId().toString() + " and t.season = "
            + sd.getId().toString() + " and t.class = '" + taskType + "'" + " and t.deleted = 0";

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();//from  ww  w  .  j a  v a2  s  . c om
    @SuppressWarnings("rawtypes")
    Iterator results = null;
    try {
        results = session.createQuery(query).iterate();
    } catch (Exception e) {
        session.getTransaction().rollback();
        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 List<Task> fetchSeasonalTasks(Hearth hearth, String taskType, SeasonDetail sd) throws Exception {
    String query = "select t from Task t " + "where t.household = " + hearth.getId().toString()
            + " and t.season = " + sd.getId().toString() + " and t.class = '" + taskType + "'"
            + " and t.deleted = 0";

    List<Task> seasonalTasks = null;

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    session.getTransaction().begin();/*from  w w w . j a  v  a  2 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) {
        seasonalTasks = new ArrayList<Task>();
        while (results.hasNext()) {
            seasonalTasks.add((Task) results.next());
        }
    }
    session.getTransaction().commit();
    return seasonalTasks;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private List<Task> fetchMultipleTasks(RestrictionList restrictions) throws Exception {
    List<Task> objects = null;
    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {//from   w w w .j  ava  2 s. c  om
        session.getTransaction().begin();
        Criteria crit = session.createCriteria(Task.class);
        for (Criterion c : restrictions) {
            crit.add(c);
        }
        objects = crit.list();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return objects;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private List<Task> fetchMultipleTasks(RestrictionList restrictions, OrderList orders) throws Exception {
    List<Task> objects = null;
    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    try {//  ww  w .  j  a v  a 2 s.c  o m
        session.getTransaction().begin();
        Criteria crit = session.createCriteria(Task.class);
        for (Criterion c : restrictions) {
            crit.add(c);
        }
        for (Order order : orders) {
            crit.addOrder(order);
        }
        objects = crit.list();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return objects;
}

From source file:uk.ac.sussex.modeltest.PlayerTest.java

License:Open Source License

@Test
public void TestHibernateCreation() {
    @SuppressWarnings("deprecation")
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    @SuppressWarnings("unused")
    Transaction tx = session.beginTransaction();
    Player player;/*w  ww .  j  av  a  2 s  .c  o  m*/
    player = (Player) session.get(Player.class, "fred");
    assertNotNull(player);
    assertEquals("fred", player.getLoginName());
}

From source file:uk.ac.sussex.modeltest.PlayerTest.java

License:Open Source License

/**   @Test
   public void TestUsername(){//from  w w w  .  j av  a 2 s  . c  o m
      Player player = new Player();
      String testUsername = "Fred";
      player.setUsername(testUsername);
      assertEquals(testUsername, player.getUsername());
   }
   **/
@Test
public void testPlayerSave() {

    String testName = "TestPlayer1";
    String newPassword = "test1";

    Player player1 = new Player();
    player1.setLoginName(testName);
    player1.setLoginPassword(newPassword);
    try {
        player1.save();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected error: " + e.getMessage());
    }

    SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    @SuppressWarnings("unused")
    Transaction tx = session.beginTransaction();

    Player player2 = (Player) session.get(Player.class, testName);

    assertEquals(player1.toString(), player2.toString());
}