@SuppressWarnings("unchecked")
  public static void main(String[] args) {
    SessionFactory factory = null;
    Session session = null;
    org.hibernate.Transaction tx = null;

    try {
      factory = HibernateUtil.getSessionFactory();
      session = factory.openSession();
      tx = session.beginTransaction();

      List<Transaction> transactions =
          session.createCriteria(Transaction.class).addOrder(Order.desc("title")).list();

      for (Transaction t : transactions) {
        System.out.println(t.getTitle());
      }

      tx.commit();

    } catch (Exception e) {
      e.printStackTrace();
      tx.rollback();
    } finally {
      session.close();
      factory.close();
    }
  }
Example #2
0
  @SuppressWarnings("unchecked")
  public static void main(String[] args) {

    SessionFactory factory = null;
    Session session = null;
    org.hibernate.Transaction tx = null;

    try {
      factory = HibernateUtil.getSessionFactory();
      session = factory.openSession();
      tx = session.beginTransaction();

      Query query = session.createQuery("select t from Transaction t");

      List<Transaction> transactions = query.list();

      for (Transaction transaction : transactions) {
        System.out.println(transaction.getTitle());
      }

      tx.commit();
    } catch (Exception e) {
      tx.rollback();
    } finally {
      session.close();
      factory.close();
    }
  }