Example usage for org.hibernate Criteria createAlias

List of usage examples for org.hibernate Criteria createAlias

Introduction

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

Prototype

public Criteria createAlias(String associationPath, String alias) throws HibernateException;

Source Link

Document

Join an association, assigning an alias to the joined association.

Usage

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

@Override
public PaginationData<SellerInvoice> get(User user, int first, int items) {
    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId()));

    criteria.setFirstResult(first);// w  ww  .  ja v  a 2  s  . c om
    criteria.setMaxResults(items);

    Set<SellerInvoice> sellerInvoices = new HashSet<>(criteria.list());
    HibernateInitSupport.setCls(SellerInvoice.class);
    for (SellerInvoice invoice : sellerInvoices)
        HibernateInitSupport.initDomain(invoice);

    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, sellerInvoices);

    return paginationData;
}

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

@Override
public SellerInvoice get(User user, int id) {
    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId()))
            .add(Restrictions.eq("id", id));

    Set<SellerInvoice> sellerInvoices = new HashSet<>(criteria.list());
    HibernateInitSupport.setCls(SellerInvoice.class);
    for (SellerInvoice invoice : sellerInvoices)
        HibernateInitSupport.initDomain(invoice);

    return sellerInvoices.iterator().next();
}

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

private Object[] filterCriteria(SellerInvoiceFilterForm form, int first, int items, int id) {
    String keyword = form.getKeyword();
    SellerInvoiceFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    Double fromPrice = form.getFromPrice();
    Double toPrice = form.getToPrice();
    SellerInvoiceOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc");

    if (keyword != null) {
        keyword = "%" + keyword + "%";
        if (searchBy == SellerInvoiceFilterType.BUYER)
            criteria.add(Restrictions.like("acc.username", keyword));
        else if (searchBy == SellerInvoiceFilterType.OWNER) {
            Name name = new Name();
            name.setName(keyword);/*from ww  w  .j  ava 2s . c  o m*/
            criteria.add(Restrictions.like("bye.name", name));
        } else if (searchBy == SellerInvoiceFilterType.ADDRESS) {
            Address address = new Address();
            address.setAddress(keyword);
            criteria.add(Restrictions.like("contact.address", address));
        } else if (searchBy == SellerInvoiceFilterType.PHONE) {
            PhoneNumber phone = new PhoneNumber();
            phone.setPhoneNumber(keyword);
            criteria.add(Restrictions.like("contact.phoneNumber", phone));
        }
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    if (fromPrice != null)
        criteria.add(Restrictions.ge("totalPrice", fromPrice));
    if (toPrice != null)
        criteria.add(Restrictions.le("totalPrice", toPrice));

    String propertyName = null;
    if (orderBy == SellerInvoiceOrderType.BUYER)
        propertyName = "acc.username";
    else if (orderBy == SellerInvoiceOrderType.OWNER)
        propertyName = "bye.name";
    else if (orderBy == SellerInvoiceOrderType.DATE)
        propertyName = "createdDate";
    else if (orderBy == SellerInvoiceOrderType.PRICE)
        propertyName = "totalPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("sl.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellerInvoice.class);
    subCriteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

private Object[] genericFilter(SellingPostFilterForm criteriaForm, int first, int items, int id) {
    Criteria criteria = getSession().createCriteria(SellingPost.class);

    //parse form/*from   ww  w  .  j a  v a 2 s  .c o  m*/
    SellingPostFilterForm form = criteriaForm;

    //get form data
    String keyword = form.getKeyword();
    Double minPrice = form.getMinPrice();
    Double maxPrice = form.getMaxPrice();
    QuatityFilterType quatityStatus = form.getQuatityStatus();
    SellingPostFilterType searchBy = form.getSearchBy();
    SellingPostStatus sellingPostStatus[] = form.getSellingPostStatus();
    Set<Subject> subjects = form.getSubjects();
    Integer subjectIds[] = new Integer[subjects.size()];
    Subject subjectsArray[] = subjects.toArray(new Subject[subjectIds.length]);
    for (int i = 0; i < subjects.size(); i++)
        subjectIds[i] = subjectsArray[i].getId();

    //create criteria
    criteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc");

    //search by keyword
    if (keyword != null && !keyword.isEmpty()) {
        keyword = "%" + keyword.trim() + "%";
        if (searchBy == SellingPostFilterType.HEADER)
            criteria.add(Restrictions.like("header", keyword));
        else if (searchBy == SellingPostFilterType.NAME) {
            criteria.add(Restrictions.like("bk.name", keyword)); //product
        } else if (searchBy == SellingPostFilterType.PUBLISHER) {
            criteria.add(Restrictions.like("pub.name", keyword));
        }
    }

    //search with price range        
    criteria.add(Restrictions.between("bk.sellingPrice", minPrice == null ? 0 : minPrice,
            maxPrice == null ? Integer.MAX_VALUE : maxPrice));

    //search with quatity status
    if (quatityStatus == QuatityFilterType.AVAILABLE)
        criteria.add(Restrictions.gt("bk.quatity", 0));
    else if (quatityStatus == QuatityFilterType.OUTOFSTOCK)
        criteria.add(Restrictions.eq("bk.quatity", 0));

    //search with selling post status
    if (sellingPostStatus.length != 0)
        criteria.add(Restrictions.in("status", sellingPostStatus));

    //search with subjects
    if (subjectIds.length != 0)
        criteria.add(Restrictions.in("sbj.id", subjectIds));

    //get data from form
    SortType sortType = form.getSortType();
    SellingPostOrderType sortByProperty = form.getSortByProperty();

    //Set up criteria
    String propertyName = null;
    if (sortByProperty == SellingPostOrderType.HEADER)
        propertyName = "header";
    else if (sortByProperty == SellingPostOrderType.NAME)
        propertyName = "bk.name";
    else if (sortByProperty == SellingPostOrderType.PUBLISHER)
        propertyName = "pub.name";
    else if (sortByProperty == SellingPostOrderType.CREATEDDATE)
        propertyName = "createdDate";
    else if (sortByProperty == SellingPostOrderType.PRICE)
        propertyName = "bk.sellingPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("sl.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> get(User user, int first, int items) {
    Criteria criteria = getSession().createCriteria(SellingPost.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId()));

    criteria.setFirstResult(first);//from  www . j av  a  2 s .c  om
    criteria.setMaxResults(items);

    List<SellingPost> posts = criteria.list();
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult(), items, first, posts);

    return paginationData;
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public SellingPost get(User user, Serializable id) {
    Criteria criteria = getSession().createCriteria(SellingPost.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("id", id))
            .add(Restrictions.eq("sl.id", user.getId()));

    List<SellingPost> posts = criteria.list();
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    return posts.iterator().next();
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> getConfirmedPost(int first, int items) {
    SQLQuery sqlQuery = getSession().createSQLQuery("");

    Criteria criteria = getSession().createCriteria(SellingPost.class)
            .add(Restrictions.eq("status", SellingPostStatus.CONFIRM));

    //Ly s dng//www.j av  a 2 s . com
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).addOrder(Order.desc("id"))
            .setFirstResult(first).setMaxResults(items);

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))).addOrder(Order.desc("id"));

    Set<SellingPost> posts = new HashSet(subCriteria.list());
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(rowCount, items, first, posts);

    return paginationData;
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> getBySubject(int first, int items, Subject subject) {
    Integer subjectId = subject.getId();
    Criteria criteria = getSession().createCriteria(SellingPost.class);
    criteria.createAlias("purchasingSellingEntity", "slen").createAlias("slen.subjects", "sbj")
            .add(Restrictions.eq("status", SellingPostStatus.CONFIRM))
            .add(Restrictions.eq("sbj.id", subjectId));

    //Ly s dng//www  .j  a  v  a 2  s.  c  o m
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).addOrder(Order.desc("id"))
            .setFirstResult(first).setMaxResults(items);

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))).addOrder(Order.desc("id"));

    Set<SellingPost> posts = new HashSet<>(subCriteria.list());
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(rowCount, items, first, posts);

    return paginationData;
}

From source file:com.bookselling.dao.SystemInvoiceDaoImpl.java

@Override
public PaginationData<SystemInvoice> filter(SystemInvoiceFilterForm form, int first, int items) {
    String keyword = form.getKeyword();
    SystemInvoiceFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    SystemInvoiceOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(SystemInvoice.class);
    criteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc");

    if (keyword == null) {
        keyword = "%" + keyword + "%";
        if (searchBy == SystemInvoiceFilterType.ACCOUNT)
            criteria.add(Restrictions.like("acc.username", keyword));
        else if (searchBy == SystemInvoiceFilterType.POSTER) {
            Name name = new Name();
            name.setName(keyword);// w  ww .  java2s.c om
            criteria.add(Restrictions.like("pst.name", name));
        } else if (searchBy == SystemInvoiceFilterType.POST_HEADER)
            criteria.add(Restrictions.like("ps.header", keyword));
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    String propertyName = null;
    if (orderBy == SystemInvoiceOrderType.ACCOUNT)
        propertyName = "acc.username";
    else if (orderBy == SystemInvoiceOrderType.POSTER)
        propertyName = "pst.name";
    else if (orderBy == SystemInvoiceOrderType.POST_HEADER)
        propertyName = "ps.header";
    else if (orderBy == SystemInvoiceOrderType.DATE)
        propertyName = "createdDate";

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SystemInvoice.class);
    subCriteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    Set<SystemInvoice> invoices = new LinkedHashSet<>(subCriteria.list());
    HibernateInitSupport.setCls(SystemInvoice.class);
    for (SystemInvoice invoice : invoices)
        HibernateInitSupport.initDomain(invoice);

    PaginationData paginationData = new PaginationData(rowCount, items, first, invoices);

    return paginationData;
}

From source file:com.bookselling.dao.TradeDaoImpl.java

@Override
public PaginationData<Trade> get(User user, int first, int items) {
    Criteria criteria = getSession().createCriteria(Trade.class);
    criteria.createAlias("buyer", "byer").add(Restrictions.eq("byer.id", user.getId()));

    criteria.setFirstResult(first);//from   w ww .j a v  a  2  s  .  co  m
    criteria.setMaxResults(items);

    Set<Trade> trades = new HashSet<>(criteria.list());
    HibernateInitSupport.setCls(Trade.class);
    for (Trade trade : trades)
        HibernateInitSupport.initDomain(trade);

    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, trades);

    return paginationData;
}