Example usage for org.hibernate SQLQuery setComment

List of usage examples for org.hibernate SQLQuery setComment

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setComment.

Prototype

@Override
    NativeQuery<T> setComment(String comment);

Source Link

Usage

From source file:org.jboss.seam.wiki.plugin.blog.BlogDAO.java

License:LGPL

public List<BlogEntry> findBlogEntriesInDirectory(WikiDirectory startDir, WikiDocument ignoreDoc, Pager pager,
        Integer year, Integer month, Integer day, String tag, boolean countComments) {

    final Map<Long, BlogEntry> blogEntryMap = new HashMap<Long, BlogEntry>();

    StringBuilder queryString = new StringBuilder();
    queryString.append("select").append(" ");
    for (int i = 0; i < getWikiDocumentSQLColumnNames().length; i++) {
        queryString.append(getWikiDocumentSQLColumnNames()[i]);
        if (i != getWikiDocumentSQLColumnNames().length - 1)
            queryString.append(", ");
    }/*from  www.ja v a 2s  .  co m*/
    queryString.append(", '0' as COMMENT_COUNT").append(" ");
    queryString.append(getblogEntryFromClause(tag));
    queryString.append(getBlogEntryWhereClause(ignoreDoc, year, month, day, tag));

    queryString.append(" ");
    queryString.append("order by doc2.CREATED_ON desc");

    SQLQuery query = getSession().createSQLQuery(queryString.toString());

    bindBlogEntryWhereClause(query, startDir, ignoreDoc, year, month, day, tag);

    query.setComment("Finding all blogEntry documents recursively in dir: " + startDir.getName());
    query.addEntity(WikiDocument.class);
    query.addScalar("COMMENT_COUNT", Hibernate.LONG);
    query.setFirstResult(pager.getQueryFirstResult());
    query.setMaxResults(pager.getQueryMaxResults());

    query.setResultTransformer(new ResultTransformer() {
        public Object transformTuple(Object[] result, String[] aliases) {
            BlogEntry be = new BlogEntry();
            be.setEntryDocument((WikiDocument) result[0]);
            blogEntryMap.put(be.getEntryDocument().getId(), be); // Put in map so we can attach comment count later
            return be;
        }

        public List transformList(List list) {
            return list;
        }
    });

    List<BlogEntry> result = (List<BlogEntry>) query.list();

    if (countComments && result.size() > 0) {
        // The risk here is that pager.getQueryMaxResults() is too large for the IN() operator of some DBs...
        StringBuilder commentQueryString = new StringBuilder();
        commentQueryString.append("select doc.NODE_ID as DOC_ID, count(c3.NODE_ID) as COMMENT_COUNT")
                .append(" ");
        commentQueryString.append("from WIKI_DOCUMENT doc").append(" ");
        commentQueryString.append("left outer join WIKI_NODE c1 on doc.NODE_ID = c1.PARENT_NODE_ID")
                .append(" ");
        commentQueryString.append("left outer join WIKI_COMMENT c2 on c1.NODE_ID = c2.NODE_ID").append(" ");
        commentQueryString.append("left outer join WIKI_COMMENT c3 on c2.NS_THREAD = c3.NS_THREAD").append(" ");
        commentQueryString.append("where doc.NODE_ID in (:blogEntriesIds)").append(" ");
        commentQueryString.append("group by doc.NODE_ID");

        SQLQuery commentQuery = getSession().createSQLQuery(commentQueryString.toString());
        commentQuery.setComment("Finding comment count for blog entries");
        commentQuery.addScalar("DOC_ID");
        commentQuery.addScalar("COMMENT_COUNT");
        commentQuery.setParameterList("blogEntriesIds", blogEntryMap.keySet());

        commentQuery.setResultTransformer(new ResultTransformer() {
            public Object transformTuple(Object[] result, String[] aliases) {
                BlogEntry be = blogEntryMap.get(((BigInteger) result[0]).longValue());
                be.setCommentCount(((BigInteger) result[1]).longValue());
                return null;
            }

            public List transformList(List list) {
                return list;
            }
        });
        commentQuery.list();
    }

    return result;
}

From source file:org.jboss.seam.wiki.plugin.blog.BlogDAO.java

License:LGPL

private List<BlogEntryCount> countBlogEntries(WikiDirectory startDir, WikiDocument ignoreDoc,
        final boolean projectYear, final boolean projectMonth, final boolean projectDay, Integer limitYear,
        Integer limitMonth, Integer limitDay, String tag) {

    // Sanity input check
    if (projectDay && (!projectMonth || !projectYear))
        throw new IllegalArgumentException("Can't project on day without months or year");
    if (projectMonth && !projectYear)
        throw new IllegalArgumentException("Can't project on month without year");

    StringBuilder queryString = new StringBuilder();

    queryString.append("select count(doc.NODE_ID) as NUM_OF_ENTRIES").append(" ");
    if (projectYear)
        queryString.append(", ").append("year(doc2.CREATED_ON) as YEAR");
    if (projectMonth)
        queryString.append(", ").append("month(doc2.CREATED_ON) as MONTH");
    if (projectDay)
        queryString.append(", ").append("day(doc2.CREATED_ON) as DAY");
    queryString.append(" ");

    queryString.append(getblogEntryFromClause(tag));
    queryString.append(getBlogEntryWhereClause(ignoreDoc, limitYear, limitMonth, limitDay, tag));

    if (projectYear || projectMonth || projectDay)
        queryString.append("group by").append(" ");
    if (projectYear)
        queryString.append("year(doc2.CREATED_ON)");
    if (projectMonth)
        queryString.append(", month(doc2.CREATED_ON)");
    if (projectDay)
        queryString.append(", day(doc2.CREATED_ON)");

    if (projectYear || projectMonth || projectDay)
        queryString.append("order by").append(" ");
    if (projectYear)
        queryString.append("YEAR desc");
    if (projectMonth)
        queryString.append(", MONTH desc");
    if (projectDay)
        queryString.append(", DAY desc");

    SQLQuery query = getSession().createSQLQuery(queryString.toString());

    bindBlogEntryWhereClause(query, startDir, ignoreDoc, limitYear, limitMonth, limitDay, tag);

    query.setComment("Finding blogEntry counts");
    query.addScalar("NUM_OF_ENTRIES", Hibernate.LONG);
    if (projectYear)
        query.addScalar("YEAR", Hibernate.INTEGER);
    if (projectMonth)
        query.addScalar("MONTH", Hibernate.INTEGER);
    if (projectDay)
        query.addScalar("DAY", Hibernate.INTEGER);

    query.setResultTransformer(new ResultTransformer() {
        public Object transformTuple(Object[] result, String[] aliases) {
            BlogEntryCount beCount = new BlogEntryCount();
            beCount.setNumOfEntries((Long) result[0]);
            if (projectYear)
                beCount.setYear((Integer) result[1]);
            if (projectMonth)
                beCount.setMonth((Integer) result[2]);
            if (projectDay)
                beCount.setDay((Integer) result[3]);
            return beCount;
        }/*from  www . ja  v a 2  s . c o  m*/

        public List transformList(List list) {
            return list;
        }
    });

    return (List<BlogEntryCount>) query.list();
}