public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    Account account = createNewAccount();
    Transaction trans1 = createNewBeltPurchase(account);
    Transaction trans2 = createShoePurchase(account);
    account.getTransactions().add(trans1);
    account.getTransactions().add(trans2);

    System.out.println(session.contains(account));
    System.out.println(session.contains(trans1));
    System.out.println(session.contains(trans1));

    try {
      org.hibernate.Transaction transaction = session.beginTransaction();
      session.save(account);

      System.out.println(session.contains(account));
      System.out.println(session.contains(trans1));
      System.out.println(session.contains(trans1));

      transaction.commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      session.close();
      HibernateUtil.getSessionFactory().close();
    }
  }
  @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 distinct t.account from Transaction t"
      //				+ " where t.amount > 500 and lower(t.transactionType) = 'deposit'");

      Query query = session.getNamedQuery("Account.largeDeposits");
      List<Account> accounts = query.list();

      for (Account a : accounts) {
        System.out.println(a.getName());
      }

      tx.commit();
    } catch (Exception e) {
      e.printStackTrace();
      tx.rollback();
    } finally {
      session.close();
      factory.close();
    }
  }
 private static Account createNewAccount() {
   Account account = new Account();
   account.setCloseDate(new Date());
   account.setOpenDate(new Date());
   account.setCreatedBy("Kevin Bowersox");
   account.setInitialBalance(new BigDecimal("50.00"));
   account.setName("Savings Account");
   account.setCurrentBalance(new BigDecimal("100.00"));
   account.setLastUpdatedBy("Kevin Bowersox");
   account.setLastUpdatedDate(new Date());
   account.setCreatedDate(new Date());
   return account;
 }