Example usage for org.hibernate Session createSQLQuery

List of usage examples for org.hibernate Session createSQLQuery

Introduction

In this page you can find the example usage for org.hibernate Session createSQLQuery.

Prototype

@Override
    NativeQuery createSQLQuery(String queryString);

Source Link

Usage

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

/**
 select IT.*, count(IT.count_purchase) as CT from ps_item as IT
where IT.id_category = ?//from w w w .  j  av a 2  s . com
order by IT.time_created desc
limit 0, ?
        
 */
@SuppressWarnings("unchecked")
@Override
public List<PsItem> selectLatest(final int categoryID, final int limit) {
    return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

        @Override
        public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {

            SQLQuery query = session
                    .createSQLQuery(" select IT.*, count(IT.count_purchase) as CT from ps_item as IT "
                            + " where IT.id_category = ? " + " order by IT.time_created desc "
                            + " limit 0, ? ");
            query.addEntity(PsItem.class);
            query.setInteger(0, categoryID);
            query.setInteger(1, limit);
            return query.list();
        }
    });
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

/**
select IT.*, count(IT.count_purchase) as CT from ps_item as IT
where IT.id_category = ?/*from  w  w w.ja va2 s .  com*/
order by CT desc
limit 0, ? 
 */
@SuppressWarnings("unchecked")
@Override
public List<PsItem> selectBestSellers(final int categoryID, final int limit) {

    return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

        @Override
        public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {

            SQLQuery query = session
                    .createSQLQuery(" select IT.*, count(IT.count_purchase) as CT from ps_item as IT "
                            + " where IT.id_category = ? " + " order by CT desc " + " limit 0, ? ");
            query.addEntity(PsItem.class);
            query.setInteger(0, categoryID);
            query.setInteger(1, limit);
            return query.list();
        }
    });
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  w w .j a v  a2  s  .c o m
public List<PsItem> searchByKeyword(final String q) {

    return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

        @Override
        public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {

            SQLQuery query = session.createSQLQuery(" SELECT IT.*, "
                    + " MATCH (`name`, `introduction`) AGAINST (? IN NATURAL LANGUAGE MODE) AS relevance "
                    + " FROM `ps_item` as IT " + " ORDER BY relevance DESC");
            query.setString(0, q);
            query.addEntity(PsItem.class);
            return query.list();
        }
    });
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w  ww.  ja v a2 s.  c o  m*/
public List<PsItem> findByPriceInCategory(final Integer psCategoryId, final Double priceMin,
        final Double priceMax, final FetchOption option) {
    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {
            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery("select * from ps_item as P where P.id_category = ? "
                        + " and P.price > ? and P.price < ? " + "order by price "
                        + (option.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?, ?");

                query.setInteger(0, psCategoryId).setDouble(1, priceMin).setDouble(2, priceMax)
                        .setInteger(3, option.offset).setInteger(4, option.limit);

                return query.addEntity(PsItem.class).list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByPriceInCategory failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w w  w  . j  ava 2s .c  o  m
public List<PsItem> findByCountPurchaseInCategory(final Integer psCategoryId, final FetchOption op) {
    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {
            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(
                        "select * from ps_item as P where P.id_category = ? " + "order by count_purchase "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?, ?");

                query.setInteger(0, psCategoryId).setInteger(1, op.offset).setInteger(2, op.limit);
                query.addEntity(PsItem.class);

                return query.list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByCountPurchaseInCategory failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  ww w .ja v a  2 s. c  o  m
public List<PsItem> findByCountFavouriteInCategory(final Integer psCategoryId, final FetchOption op) {

    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(
                        "select * from ps_item as P where P.id_category = ? " + "order by count_favourite "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + " limit ?, ?");

                query.setInteger(0, psCategoryId).setInteger(1, op.offset).setInteger(2, op.limit);
                query.addEntity(PsItem.class);

                return query.list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByCountFavouriteInCategory failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   ww w.  java2s .  c  om
public List<PsItem> findByVoteInCategory(final Integer psCategoryId, final FetchOption op) {

    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session
                        .createSQLQuery("select * from ps_item as P where P.id_category = ? " + "order by vote "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?, ?");

                query.setInteger(0, psCategoryId).setInteger(1, op.offset).setInteger(2, op.limit);
                query.addEntity(PsItem.class);

                return query.list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByVoteInCategory failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w  ww.  j  a  va 2 s.  co m
public List<PsItem> findByPriceInTag(final Integer psTagId, final Double priceMin, final Double priceMax,
        final FetchOption op) {
    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(
                        "select P.* from ps_item as P where P.id in (select R.id_item from r_tag_item as R where R.id_tag = ? ) "
                                + " and P.price > ? and P.price < ? " + "order by price "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?,  ?");

                query.setInteger(0, psTagId).setDouble(1, priceMin).setDouble(2, priceMax)
                        .setInteger(3, op.offset).setInteger(4, op.limit);

                return query.addEntity(PsItem.class).list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByPriceInTag failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w  w w . j  ava2  s.c  o m*/
public List<PsItem> findByCountPurchaseInTag(final Integer psTagId, final FetchOption op) {
    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(
                        "select P.* from ps_item as P where P.id in (select R.id_item from r_tag_item as R where R.id_tag = ? ) "
                                + "order by count_purchase "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?, ?");

                query.setInteger(0, psTagId).setInteger(1, op.offset).setInteger(2, op.limit);

                return query.addEntity(PsItem.class).list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByCountPurchaseInTag failed", re);
        throw re;
    }
}

From source file:com.bitranger.parknshop.common.dao.impl.PsItemDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w w  w.  j  a  va  2s  . c  o  m
public List<PsItem> findByCountFavouriteInTag(final Integer psTagId, final FetchOption op) {
    try {
        return getHibernateTemplate().executeFind(new HibernateCallback<List<PsItem>>() {

            @Override
            public List<PsItem> doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(
                        "select P.* from ps_item as P where P.id in (select R.id_item from r_tag_item as R where R.id_tag = ? ) "
                                + "order by count_favourite "
                                + (op.sortOption == SortOption.ASCENDING ? "asc" : "desc ") + "limit ?, ?");

                query.setInteger(0, psTagId).setInteger(1, op.offset).setInteger(2, op.limit);

                return query.addEntity(PsItem.class).list();
            }
        });
    } catch (RuntimeException re) {
        log.error("find by findByCountFavouriteInTag failed", re);
        throw re;
    }
}