Example usage for org.hibernate Criteria setCacheable

List of usage examples for org.hibernate Criteria setCacheable

Introduction

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

Prototype

public Criteria setCacheable(boolean cacheable);

Source Link

Document

Enable caching of this query result, provided query caching is enabled for the underlying session factory.

Usage

From source file:com.quix.aia.cn.imo.mapper.AamDataMaintenance.java

License:Open Source License

/**
 * <p>//from   w  w w . j av  a2  s. com
 * This method retrieves Id for particular agentId
 * </p>
 * 
 * @param agentId
 * @return Integer 
 * 
 */
public static Integer checkIfAgentExist(String agentId) {
    Integer id = 0;
    try {
        session = clientSessionFactory.openSession();
        transaction = session.beginTransaction();

        Criteria crit = session.createCriteria(AamData.class);
        if (null != agentId && !"".equals(agentId)) {
            //crit.setProjection(Projections.property("id"));
            crit.add(Restrictions.eq("agentCode", agentId));
        }

        ArrayList list = (ArrayList) crit.setCacheable(true).list();
        if (list != null && list.size() > 0)
            id = 1;
        else
            id = 0;
    } catch (Exception he) {
        he.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        he.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AamDataMaintenance", Level.SEVERE + "", errors.toString());
    } finally {
        session.close();
    }

    return id;
}

From source file:com.quix.aia.cn.imo.mapper.AddressBookMaintenance.java

License:Open Source License

/**
 * <p>//from   w w w . j  a v  a 2s .com
 * This method retrieves AddressBook of Particular nric code
 * <p>
 * 
 * @param iosAddressCode and agentId
 * @return AddressBook Class Object
 * 
 */
public AddressBook getAddressBookNric(String nric, String agentId) {

    Session session = null;
    Criteria criteria = null;
    ArrayList<AddressBook> list = null;

    try {
        session = HibernateFactory.openSession();
        criteria = session.createCriteria(AddressBook.class);
        criteria.add(Restrictions.eq("nric", nric));
        criteria.add(Restrictions.eq("agentId", agentId));
        list = (ArrayList) criteria.setCacheable(true).list();

    } catch (Exception ex) {
        log.log(Level.SEVERE, ex.getMessage());
        ex.printStackTrace();
        ex.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        ex.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AddressBookMaintenance", Level.SEVERE + "", errors.toString());
    } finally {
        try {
            HibernateFactory.close(session);
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage());
            e.printStackTrace();
        }
        session = null;
        criteria = null;
    }

    if (null == list) {
        list = new ArrayList<AddressBook>();
    }

    return list.get(0);
}

From source file:com.quix.aia.cn.imo.mapper.AddressBookMaintenance.java

License:Open Source License

/**
 * <p>/*from  ww w  .  ja  v a 2  s.  co m*/
 * This method get AddressBook of Particular address code
 * <p>
 * 
 * @param address code
 * @return AddressBook Class Object
 * 
 */
public AddressBook getAddressBook(AddressBook addressBook) {

    Session session = null;
    Criteria criteria = null;
    ArrayList<AddressBook> list = null;

    try {
        session = HibernateFactory.openSession();
        criteria = session.createCriteria(AddressBook.class);
        criteria.add(Restrictions.eq("addressCode", addressBook.getAddressCode()));
        list = (ArrayList) criteria.setCacheable(true).list();

    } catch (Exception ex) {
        log.log(Level.SEVERE, ex.getMessage());
        ex.printStackTrace();
        ex.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        ex.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AddressBookMaintenance", Level.SEVERE + "", errors.toString());
    } finally {
        try {
            HibernateFactory.close(session);
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage());
            e.printStackTrace();
        }
        session = null;
        criteria = null;
    }

    if (null == list) {
        list = new ArrayList<AddressBook>();
    }

    return list.get(0);
}

From source file:com.quix.aia.cn.imo.mapper.AddressBookMaintenance.java

License:Open Source License

public AddressBook getaddressDataForCCTest(String candidateCode) {
    // TODO Auto-generated method stub
    Session session = null;/*from   w  w w.j  a  v a 2 s . c  om*/
    Criteria criteria = null;
    ArrayList<AddressBook> list = null;
    AddressBook address = null;

    try {

        session = HibernateFactory.openSession();
        criteria = session.createCriteria(AddressBook.class);
        criteria.add(Restrictions.eq("addressCode", Integer.parseInt(candidateCode)));
        list = (ArrayList) criteria.setCacheable(true).list();
        if (list.size() > 0) {
            address = list.get(0);
        }

    } catch (Exception ex) {
        log.log(Level.SEVERE, ex.getMessage());
        ex.printStackTrace();
        ex.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        ex.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("AddressBookMaintenance", Level.SEVERE + "", errors.toString());
    } finally {
        try {
            HibernateFactory.close(session);
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage());
            e.printStackTrace();
        }
        session = null;
        criteria = null;
    }

    if (null == list) {
        list = new ArrayList<AddressBook>();
    }

    return address;
}

From source file:com.quix.aia.cn.imo.mapper.CandidateTrainingResultMaintenance.java

License:Open Source License

/**
 * <p>Insert candidate done</p>
 * @param candidate//from w  w  w .  ja  v  a  2s.com
 * @return
 */
public CandidateTrainingResult getCandidateTrainingResult(String recruiterAgentCode, String nric) {
    Integer key = 0;
    Session session = null;
    List<CandidateTrainingResult> list = new ArrayList();
    try {

        session = HibernateFactory.openSession();
        Criteria crit = session.createCriteria(CandidateTrainingResult.class);
        crit.add(Restrictions.eq("recruiterAgentCode", recruiterAgentCode));
        crit.add(Restrictions.eq("perAgentId", nric));
        list = (ArrayList<CandidateTrainingResult>) crit.setCacheable(true).list();
        log.log(Level.INFO, "---New Candidate Training Result Fetched Successfully--- ");
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        LogsMaintenance logsMain = new LogsMaintenance();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        logsMain.insertLogs("CandidateTrainingResultMaintenance", Level.SEVERE + "", errors.toString());
    } finally {
        try {
            HibernateFactory.close(session);
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage());
            e.printStackTrace();
        }
    }

    if (!list.isEmpty()) {
        return list.get(0);
    } else {
        return null;
    }
}

From source file:com.sapienter.jbilling.server.user.db.UserDAS.java

License:Open Source License

public UserDTO findRoot(String username) {
    if (username == null || username.length() == 0) {
        LOG.error("can not find an empty root: " + username);
        return null;
    }/*from   www  . j a v a2 s  .  c o m*/
    // I need to access an association, so I can't use the parent helper class
    Criteria criteria = getSession().createCriteria(UserDTO.class).add(Restrictions.eq("userName", username))
            .add(Restrictions.eq("deleted", 0)).createAlias("roles", "r")
            .add(Restrictions.eq("r.id", CommonConstants.TYPE_ROOT));

    criteria.setCacheable(true); // it will be called over an over again

    return (UserDTO) criteria.uniqueResult();
}

From source file:com.sapienter.jbilling.server.user.db.UserDAS.java

License:Open Source License

public UserDTO findWebServicesRoot(String username) {
    if (username == null || username.length() == 0) {
        LOG.error("can not find an empty root: " + username);
        return null;
    }/*from   ww  w.ja  v  a  2  s  .  c om*/
    // I need to access an association, so I can't use the parent helper class
    Criteria criteria = getSession().createCriteria(UserDTO.class).add(Restrictions.eq("userName", username))
            .add(Restrictions.eq("deleted", 0)).createAlias("roles", "r")
            .add(Restrictions.eq("r.id", CommonConstants.TYPE_ROOT)).createAlias("permissions", "p")
            .add(Restrictions.eq("p.permission.id", 120));

    criteria.setCacheable(true); // it will be called over an over again

    return (UserDTO) criteria.uniqueResult();
}

From source file:com.sapienter.jbilling.server.util.db.AbstractDAS.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<T> findByExample(T exampleInstance, String... excludeProperty) {
    Criteria crit = getSession().createCriteria(getPersistentClass());
    Example example = Example.create(exampleInstance);
    for (String exclude : excludeProperty) {
        example.excludeProperty(exclude);
    }//from  ww w .j ava 2 s  .c  o  m
    crit.add(example);
    crit.setCacheable(queriesCached);
    return crit.list();
}

From source file:com.sapienter.jbilling.server.util.db.AbstractDAS.java

License:Open Source License

@SuppressWarnings("unchecked")
public T findByExampleSingle(T exampleInstance, String... excludeProperty) {
    Criteria crit = getSession().createCriteria(getPersistentClass());
    Example example = Example.create(exampleInstance);
    for (String exclude : excludeProperty) {
        example.excludeProperty(exclude);
    }//www  .  j  a v  a 2 s  .  c  om
    crit.add(example);
    crit.setCacheable(queriesCached);
    return (T) crit.uniqueResult();
}

From source file:com.sapienter.jbilling.server.util.db.AbstractDAS.java

License:Open Source License

/**
 * Use this inside subclasses as a convenience method.
 *//*from  ww w . jav  a 2s  . c  o m*/
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
    Criteria crit = getSession().createCriteria(getPersistentClass());
    for (Criterion c : criterion) {
        crit.add(c);
    }
    crit.setCacheable(queriesCached);
    return crit.list();
}