Example usage for org.hibernate Criteria list

List of usage examples for org.hibernate Criteria list

Introduction

In this page you can find the example usage for org.hibernate Criteria list.

Prototype

public List list() throws HibernateException;

Source Link

Document

Get the results.

Usage

From source file:Activity.activityGetter.java

public static ActivityReport[] getActivityReports() {

    //create SessionFactory object for opening Session
    //SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session;//from  w ww. ja  v  a 2  s  .  c  o m
    //Criteria requires a transaction opened
    session = HibernateUtil.getSessionFactory().openSession();
    //create Criteria for Customer class
    Criteria criteria = session.createCriteria(ActivityReport.class);
    //add a Restriction which will be used for equality > state = MI
    //criteria.add(Restrictions.eq(state?, stateName));
    //add an order for using customer_ID column
    //criteria.addOrder(Order.asc(customer_ID?));
    //return the resultset as a List
    List<ActivityReport> ActivityReportresult = criteria.list();

    //convert List to Array
    return ActivityReportresult.toArray(new ActivityReport[ActivityReportresult.size()]);

}

From source file:Activity.activityGetter.java

public static Activity[] getActivitiesByCategoryId(int categoryId) {

    Session session;//from  w ww  . j a  va 2 s  .  c  o  m
    //Criteria requires a transaction opened
    session = HibernateUtil.getSessionFactory().openSession();
    //create Criteria for Customer class
    Criteria criteria = session.createCriteria(Activity.class);
    criteria.add(Restrictions.eq("category.categoryId", categoryId));
    //add a Restriction which will be used for equality > state = MI

    //add an order for using customer_ID column
    //criteria.addOrder(Order.asc(customer_ID?));
    //return the resultset as a List
    List<Activity> Activityresult = criteria.list();

    //convert List to Array
    return Activityresult.toArray(new Activity[Activityresult.size()]);
}

From source file:Activity.activityGetter.java

public static ActivityReport[] getActivityReportsByCategoryId(int categoryId) {

    Session session;/*w  ww .j  a  va 2  s. c  om*/
    //Criteria requires a transaction opened
    session = HibernateUtil.getSessionFactory().openSession();
    //create Criteria for Customer class
    Criteria criteria = session.createCriteria(ActivityReport.class);
    criteria.add(Restrictions.eq("activty.activityId.category", categoryId));
    //add a Restriction which will be used for equality > state = MI

    //add an order for using customer_ID column
    //criteria.addOrder(Order.asc(customer_ID?));
    //return the resultset as a List
    List<Activity> Activityresult = criteria.list();

    //convert List to Array
    return Activityresult.toArray(new ActivityReport[Activityresult.size()]);
}

From source file:AdminSystemClasses.AtmDB.java

public static BankATM addAtm(ATMaddmoddel atm) throws Exception {

    Session sf = DB.getSession();/*w  w  w.ja  v a  2 s  . c  o m*/
    Transaction tx = sf.beginTransaction();

    Criteria cr = sf.createCriteria(BankATM.class);
    cr.add(Restrictions.eq("id", atm.getAtmId()));

    if (cr.list().isEmpty()) {
        BankBranch bb = (BankBranch) sf.load(BankBranch.class, atm.getBranchid());
        BankATM myatm = new BankATM();
        myatm.setId(atm.getAtmId());
        myatm.setBankbranch(bb);
        myatm.setTill(atm.getTill());
        sf.save(myatm);
        tx.commit();
        return myatm;
    }

    return null;
}

From source file:AdminSystemClasses.AtmDB.java

public static BankATM delete(DeleteATM atm) throws Exception {
    Session sf = DB.getSession();//  ww w  . j  ava 2s .  com
    Transaction tx = sf.beginTransaction();

    Criteria cr = sf.createCriteria(BankATM.class);
    cr.add(Restrictions.eq("id", atm.getAtmId()));
    BankATM bank;
    if (!cr.list().isEmpty()) {
        bank = (BankATM) sf.load(BankATM.class, atm.getAtmId());
        sf.delete(bank);
        tx.commit();
    } else {
        return null;
    }

    return bank;
}

From source file:AdminSystemClasses.CustomerInfoGenerator.java

/**
 * A thing that generates values for autofill info for a BankCustomer
 * //from   w w  w.j  a  v  a  2 s  .c o m
 * @param cus BankCustomer to be autofilled
 * @return BankAccount generated and now associated to cus
 * @throws HibernateException 
 */
public static BankAccount generate(BankCustomer cus) throws HibernateException {
    Random rand = new Random(System.currentTimeMillis());
    Integer cusId;
    Session s = bank.DB.getSession();
    Criteria cr;
    List l;

    //cus.setId(1000);
    do {
        cusId = rand.nextInt(100000000);
        cr = s.createCriteria(BankCustomer.class);
        cr.add(Restrictions.eq("id", cusId));
        l = cr.list();
    } while (!l.isEmpty());

    cus.setId(cusId);

    return generateAccount(cus);
}

From source file:AdminSystemClasses.CustomerInfoGenerator.java

/**
 * A thing that generates a BankAccount for a BankCustomer
 * I made this a separate method because it would be useful to
 * have if a customer could have multiple accounts.
 * /*from   ww w .j a  v  a  2  s.  c o  m*/
 * @param cus BankCustomer to generate BankAccount for
 * @return BankAccount generated and now associated to cus
 * @throws HibernateException 
 */
private static BankAccount generateAccount(BankCustomer cus) throws HibernateException {
    Random rand = new Random(System.currentTimeMillis());
    Integer c1, c2, acctId, pin;
    String card;
    BankAccount acct;
    Session s = bank.DB.getSession();
    Criteria cr;
    List l;

    do {
        acctId = rand.nextInt(100000000);
        cr = s.createCriteria(BankAccount.class);
        cr.add(Restrictions.eq("accountid", acctId));
        l = cr.list();
    } while (!l.isEmpty());

    do {
        c1 = rand.nextInt(100000000);
        c2 = rand.nextInt(100000000);
        card = String.format("%08d", c1) + String.format("%08d", c2);
        cr = s.createCriteria(BankAccount.class);
        cr.add(Restrictions.eq("cardnumber", card));
        l = cr.list();
    } while (!l.isEmpty());

    pin = rand.nextInt(10000);

    acct = new BankAccount(acctId, cus, new BigDecimal(0), String.format("%04d", pin), card);

    cus.addBankAccount(acct);
    return acct;
}

From source file:AdminSystemClasses.EditATM.java

@Override
public String execute() {
    String msg = null;//from w w  w  .  j av  a2  s. com
    Session sf = DB.getSession();
    Transaction tx = sf.beginTransaction();
    // atm = (BankATM)sf.load(BankATM.class, getAtmId());
    Criteria cr = sf.createCriteria(BankATM.class);
    cr.add(Restrictions.eq("id", getAtmId()));
    if (cr.list().size() > 0) {
        atm = cr.list();

        msg = "success";
    } else {
        msg = "failed";
    }

    return msg;

}

From source file:agenda.Dao.DaoPessoa.java

public List consultaGrupo() {
    sessao = HibernateUtil.getSessionFactory().openSession();
    List<Grupo> g = new ArrayList<>();
    try {/*  w  ww .ja  v  a 2 s. c  o m*/
        trans = sessao.beginTransaction();
        Criteria c = sessao.createCriteria(Grupo.class);
        g = c.list();

    } catch (Exception e) {

    } finally {
        sessao.close();
    }
    return g;
}

From source file:agenda.Dao.DaoPessoa.java

public List consultaPessoa() {
    sessao = HibernateUtil.getSessionFactory().openSession();
    List<Pessoa> pe = new ArrayList<>();
    try {/*w ww  . ja  v  a2  s  .  c  o  m*/
        trans = sessao.beginTransaction();
        Criteria c = sessao.createCriteria(Pessoa.class);
        pe = c.list();
    } catch (Exception e) {

    } finally {
        sessao.close();
    }
    return pe;
}