Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.jacobheric.youbrew.dao.impl.ExpenseDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Expense> search(ExpenseCriteria expenseCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Expense.class);

    ////ww  w .  j a v  a 2s .c o  m
    //Property restrictions
    if (expenseCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    expenseCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (expenseCriteria.getStart() != null) {
        c.setFirstResult(expenseCriteria.getStart());
    }

    if (expenseCriteria.getLimit() != null) {
        c.setMaxResults(expenseCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jacobheric.youbrew.dao.impl.RecipeDAOImpl.java

License:Open Source License

/**
 * @param recipeCriteria - custom criteria object
 * @return list of recipe criteria//  ww  w.ja  va 2 s.  com
 */
@SuppressWarnings("unchecked")
public List<Recipe> search(RecipeCriteria recipeCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Recipe.class);

    //
    //Property restrictions
    if (recipeCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    recipeCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (recipeCriteria.getStart() != null) {
        c.setFirstResult(recipeCriteria.getStart());
    }

    if (recipeCriteria.getLimit() != null) {
        c.setMaxResults(recipeCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jubination.model.dao.AdminDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Object readPropertyList(Object entity, String listType) {
    Admin admin = (Admin) entity;/*from  w  w  w . j a v  a2s  .c o m*/
    List list = null;
    switch (listType) {
    case "Inbox":
        session = getSessionFactory().getCurrentSession();

        admin = (Admin) session.get(Admin.class, admin.getUsername());

        Criteria criteria = session.createCriteria(MailMessage.class, "msg");
        criteria.add(Restrictions.eq("msg.receiver.username", admin.getUsername()));
        criteria.addOrder(Order.desc("messageId"));

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.setFirstResult(0);
        criteria.setMaxResults(10);
        list = criteria.list();

        break;
    case "Sent":
        session = getSessionFactory().getCurrentSession();

        admin = (Admin) session.get(Admin.class, admin.getUsername());

        criteria = session.createCriteria(MailMessage.class, "msg");
        criteria.add(Restrictions.eq("msg.sender.username", admin.getUsername()));
        criteria.addOrder(Order.desc("messageId"));

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.setFirstResult(0);
        criteria.setMaxResults(10);
        list = criteria.list();

        break;
    default:
        System.out.println("Not an option");
        admin = null;
        break;
    }

    return (T) list;
}

From source file:com.jubination.model.dao.ClientDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED)
public List<TempClient> readClientWithStatus(String param) {
    session = getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(TempClient.class);
    criteria.add(Restrictions.eq("callStatus", param));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<TempClient> list = criteria.list();
    System.out.println("READ TEMP CLIENT WITH A STATUS :::::::::::::::::::::::::::::::::::::::::::::::CHECK");
    return list;/*from  w w  w  .  j a  va 2 s.  co  m*/

}

From source file:com.jubination.model.dao.ClientDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED)
public List<TempClient> readClientOvernight() {
    session = getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(TempClient.class);
    criteria.add(Restrictions.eq("overnight", true));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<TempClient> list = criteria.list();
    System.out.println("READ TEMP CLIENT OVERNIGHT :::::::::::::::::::::::::::::::::::::::::::::::CHECK");
    return list;//from   w w w .j a v  a 2  s  .c  o m

}

From source file:com.jubination.model.dao.ClientDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Object getByProperty(Object entity, String listType) {
    List<Client> list = new ArrayList<Client>();
    switch (listType) {
    case "Email":
        String emailId = (String) entity;
        session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(Client.class, "client");
        criteria.add(Restrictions.eq("emailId", emailId));
        list = criteria.list();/*from   w  w w  .j a va2s .  c  o m*/

        break;
    case "City":
        String city = (String) entity;
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "client");
        criteria.add(Restrictions.like("city", city, MatchMode.START));
        list = criteria.list();

        break;
    case "Id":
        Long orderId = (Long) entity;
        session = getSessionFactory().getCurrentSession();
        list.add((Client) session.get(Client.class, orderId));

        break;

    case "LeadId":
        String leadId = (String) entity;
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "client");
        criteria.add(Restrictions.eq("tempLeadDetails", leadId));
        list = criteria.list();

        break;
    case "Number":
        String number = (String) entity;
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class);
        criteria.add(Restrictions.like("phoneNumber", number, MatchMode.ANYWHERE));
        list = criteria.list();
        for (Client client : list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
            }
        }

        break;
    case "Name":
        String name = (String) entity;
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class);
        criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
        list = criteria.list();

        break;
    case "DateCreated":
        String dateCreated = (String) entity;
        session = getSessionFactory().getCurrentSession();

        criteria = session.createCriteria(Client.class);
        criteria.add(Restrictions.like("dateCreation", dateCreated, MatchMode.START));
        list = criteria.list();

        break;
    case "DateCreatedLeadProperty":
        dateCreated = (String) entity;
        session = getSessionFactory().getCurrentSession();

        criteria = session.createCriteria(Lead.class);
        criteria.createAlias("client", "c")
                .add(Restrictions.like("c.dateCreation", dateCreated, MatchMode.START));
        list = criteria.list();

        break;
    case "DateUpdatedFull":
        String dateUpdated = (String) entity;
        session = getSessionFactory().getCurrentSession();

        criteria = session.createCriteria(Client.class, "client");
        criteria.createAlias("client.lead", "l");
        criteria.createAlias("l.call", "c");
        criteria.add(Restrictions.like("c.DateUpdated", dateUpdated, MatchMode.START));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();

        for (Client client : list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                lead.getCall().size();
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
            }
        }

        break;

    default:
        System.err.println("Not a valid option");
        break;
    }
    System.out.println(
            "READ CLIENT WITH A PROPERTY (INNER AND NON INNER MIXED) :::::::::::::::::::::::::::::::::::::::::::::::CHECK");
    return list;
}

From source file:com.jubination.model.dao.ClientDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List fetchEntities(String paramVal) {
    List list = null;/*from www. j av a 2  s . c om*/

    switch (paramVal) {

    case "PendingMinusOne":
        session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.add(Restrictions.and(Restrictions.lt("l.count", 0), Restrictions.isNull("l.followUpDate"),
                Restrictions.gt("l.leadId", "50000")));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();

        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;
    case "PendingInProgress":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.createAlias("l.call", "call");
        criteria.add(Restrictions.and(Restrictions.le("l.count", 0), Restrictions.gt("l.leadId", "50000"),
                Restrictions.eq("call.Status", "in-progress")));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();

        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;
    case "PendingAndNotified":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.add(Restrictions.or(Restrictions.and(

                Restrictions.ge("l.count", 1), Restrictions.eq("l.followUpDate", ""),
                Restrictions.isNull("l.followUpDate")),

                Restrictions.and(Restrictions.ge("l.count", 1), Restrictions.ne("l.followUpDate", ""),
                        Restrictions.isNotNull("l.followUpDate"),
                        Restrictions.le("l.followUpDate",
                                new SimpleDateFormat("yyyy-MM-dd").format(new Date()))),
                Restrictions.eq("l.followUpDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date())))

        );
        criteria.addOrder(Order.desc("l.followUpDate"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();

        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;

    case "Pending":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.add(Restrictions.and(Restrictions.and(Restrictions.ge("l.count", 1),
                Restrictions.isNull("l.missedAppointment"), Restrictions.isNull("l.followUpDate"))));

        criteria.addOrder(Order.asc("l.count"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();
        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();

            }
        }
        break;
    case "Notified":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.add(
                Restrictions.or(
                        Restrictions.and(
                                Restrictions
                                        .and(Restrictions.ge("l.count", 1),
                                                Restrictions.and(
                                                        Restrictions.le("l.followUpDate",
                                                                new SimpleDateFormat("yyyy-MM-dd")
                                                                        .format(new Date())),
                                                        Restrictions.gt("l.followUpDate", "2016-01-01"))),
                                Restrictions.isNull("l.missedAppointment")),
                        Restrictions.and(
                                Restrictions.eq("l.followUpDate",
                                        new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
                                Restrictions.eq("l.leadStatus", "Follow up/Call back"))));
        criteria.addOrder(Order.asc("l.followUpDate"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();
        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;
    case "PendingMA":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class, "c");
        criteria.createAlias("c.lead", "l");
        criteria.add(Restrictions.and(Restrictions.ge("l.count", 1),
                Restrictions.eq("l.missedAppointment", true), Restrictions.isNull("l.followUpDate")));

        criteria.addOrder(Order.desc("l.count"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();
        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;
    case "NotifiedMA":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class);
        criteria.createAlias("lead", "l");
        criteria.add(Restrictions.and(
                Restrictions.and(Restrictions.ge("l.count", 1),
                        Restrictions.and(
                                Restrictions.le("l.followUpDate",
                                        new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
                                Restrictions.gt("l.followUpDate", "2016-01-01"))),
                Restrictions.eq("l.missedAppointment", true)));
        criteria.addOrder(Order.asc("l.followUpDate"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        list = criteria.list();
        for (Client client : (List<Client>) list) {
            client.getLead().size();
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
        }
        break;

    case "Overnight":
        session = getSessionFactory().getCurrentSession();
        criteria = session.createCriteria(Client.class);
        criteria.add(Restrictions.eq("overnight", true));
        list = criteria.list();
        for (Client client : (List<Client>) list) {
            for (Lead lead : client.getLead()) {
                if (lead.getBeneficiaries() != null) {
                    lead.getBeneficiaries().size();
                }
                lead.getCall().size();
            }
            client.getLead().size();
        }
        break;

    default:

        break;
    }
    if (list != null) {
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" + list.size() + "$$$" + paramVal);
    }

    System.out.println(
            "READ CLIENT WITH INNER ELEMENTS WITH STATUS :::::::::::::::::::::::::::::::::::::::::::::::CHECK");
    return list;

}

From source file:com.jubination.model.dao.ClientDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<Object> fetchFreshCallEntity(String fromDate, String toDate) {
    List list = null;/* ww w  .  j a  v a 2s. c  om*/
    session = getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(Client.class, "client").add(Restrictions.and(
            Restrictions.ge("client.dateCreation", fromDate), Restrictions.le("client.dateCreation", toDate)));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    list = criteria.list();
    for (Client c : (List<Client>) list) {
        c.getLead().size();
        for (Lead l : c.getLead()) {
            l.getCall().size();
        }
    }

    System.out.println(
            "IMPORTANT:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" + list.size());
    return list;
}

From source file:com.jubinationre.model.dao.AdminDAOImpl.java

@Override
public Object readPropertyList(Object entity, String listType) {
    Admin admin = (Admin) entity;//from   ww w  . ja v  a 2  s. com
    List list = null;
    switch (listType) {
    case "Inbox":
        try {
            session = getSessionFactory().openSession();
            session.beginTransaction();
            admin = (Admin) session.get(Admin.class, admin.getUsername());

            Criteria criteria = session.createCriteria(Message.class, "msg");
            criteria.add(Restrictions.eq("msg.receiver.username", admin.getUsername()));
            criteria.addOrder(Order.desc("messageId"));

            criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            criteria.setFirstResult(0);
            criteria.setMaxResults(10);
            list = criteria.list();
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println("Error in reading Favourites at CustomerDAO " + e);
            e.printStackTrace();
            admin = null;
        } finally {
            if (session.isOpen()) {
                session.close();
            }

        }
        break;
    case "Sent":
        try {
            session = getSessionFactory().openSession();
            session.beginTransaction();

            admin = (Admin) session.get(Admin.class, admin.getUsername());

            Criteria criteria = session.createCriteria(Message.class, "msg");
            criteria.add(Restrictions.eq("msg.sender.username", admin.getUsername()));
            criteria.addOrder(Order.desc("messageId"));

            criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            criteria.setFirstResult(0);
            criteria.setMaxResults(10);
            list = criteria.list();
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println("Error in reading Favourites at CustomerDAO " + e);
            e.printStackTrace();
            admin = null;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        break;
    default:
        System.out.println("Not an option");
        admin = null;
        break;
    }

    return (T) list;
}

From source file:com.lakeside.data.sqldb.PageBaseDao.java

License:Apache License

/**
 * countCriteria./*w w  w  . java  2s .c o m*/
 */
@SuppressWarnings("unchecked")
protected int countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List<CriteriaImpl.OrderEntry>) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList<Object>());
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    // Count
    int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    return totalCount;
}