hihi ... l have a funny solution ,
my DAO - HiberBookDao.java
to be continue...Code:.. public Map findBooksByCriteriaWithTotal(BookCommand bookCommand) throws DataAccessException { ArrayList books = (ArrayList) findBooksByCriteriaWithLimit(bookCommand); Integer totalFound = findBooksTotalByCriteria(bookCommand); Map model = new HashMap(); model.put("_totalElements", totalFound); model.put("_resultset", books); return model; } private BookCommand makeMCommand(BookCommand source){ int m = (source.getS_page() == 0)? 0 : source.getS_page()-1; BookCommand target = new BookCommand(); BeanUtils.copyProperties(source,target); target.setS_page(m); return target; } public Collection findBooksByCriteriaWithLimit(BookCommand bookCommand) throws DataAccessException { final int page = bookCommand.getS_page(); final BookCommand nbookCommand = bookCommand; final BookCommand mbookCommand = makeMCommand(bookCommand); final Book book = bookCommand.getBook(); final Author iAuthor = bookCommand.getAuthor(); final Publisher iPublisher = book.getPublisher(); return getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { StringBuffer queryString = new StringBuffer(); boolean conditionFound = false; String title = book.getTitle(); String isbn = book.getIsbn(); String authorName = iAuthor.getAuthorName(); String publisherName = iPublisher.getPublisherName(); if(title != null){ queryString.append("lower(b.title) like :title "); conditionFound=true; } if(isbn != null){ if (conditionFound) queryString.append("and "); queryString.append("lower(b.isbn) like :isbn "); conditionFound=true; } if (authorName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(a.authorName) like :authorName "); conditionFound=true; } if (publisherName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(p.publisherName) like :publisherName "); conditionFound=true; } String fromClause = conditionFound ? "from Book b " + "left join fetch b.authors a " + "left join fetch b.publisher p where " : "from Book b " + "left join fetch b.authors a " + "left join fetch b.publisher p "; queryString.insert(0, fromClause).append("order by b.title"); Query query = getSession().createQuery( queryString.toString() ); if (title != null) query.setString( "title",'%' + title.toLowerCase() + '%' ); if (isbn != null) query.setString( "isbn",'%' + isbn.toLowerCase() + '%' ); if (authorName != null) query.setString( "authorName",'%' + authorName.toLowerCase() + '%' ); if (publisherName != null) query.setString( "publisherName",'%' + publisherName.toLowerCase() + '%' ); if(page == 0){ query.setFirstResult(0) .setMaxResults(pageNodeByCriteria(nbookCommand).intValue()); }else{ query.setFirstResult(pageNodeByCriteria(mbookCommand).intValue()) .setMaxResults(pageNodeByCriteria(nbookCommand).intValue() - pageNodeByCriteria(mbookCommand).intValue()); } Set distinctResults = new HashSet(query.list()); List result = new ArrayList(distinctResults); Collections.sort(result,new BookNameComparator()); return result; } }); } public Integer pageNodeByCriteria(BookCommand bookCommand) throws DataAccessException { final Book book = bookCommand.getBook(); final Author iAuthor = bookCommand.getAuthor(); final Publisher iPublisher = book.getPublisher(); final int page = bookCommand.getS_page(); final int pageSize = bookCommand.getS_pageSize(); return (Integer) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { StringBuffer queryString = new StringBuffer(); boolean conditionFound = false; String title = book.getTitle(); String isbn = book.getIsbn(); String authorName = iAuthor.getAuthorName(); String publisherName = iPublisher.getPublisherName(); if(title != null){ queryString.append("lower(b.title) like :title "); conditionFound=true; } if(isbn != null){ if (conditionFound) queryString.append("and "); queryString.append("lower(b.isbn) like :isbn "); conditionFound=true; } if (authorName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(a.authorName) like :authorName "); conditionFound=true; } if (publisherName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(p.publisherName) like :publisherName "); conditionFound=true; } String fromClause = conditionFound ? "select count(*) from Book b " + "left join b.authors a " + "left join b.publisher p where " : "select count(*) from Book b " + "left join b.authors a " + "left join b.publisher p "; queryString.insert(0, fromClause).append("group by b.id order by b.title"); Query query = getSession().createQuery( queryString.toString() ); if (title != null) query.setString( "title",'%' + title.toLowerCase() + '%' ); if (isbn != null) query.setString( "isbn",'%' + isbn.toLowerCase() + '%' ); if (authorName != null) query.setString( "authorName",'%' + authorName.toLowerCase() + '%' ); if (publisherName != null) query.setString( "publisherName",'%' + publisherName.toLowerCase() + '%' ); Iterator iter = query.setFirstResult(0) .setMaxResults((page+1) * pageSize) .list() .iterator(); int sum = 0; while ( iter.hasNext() ) { Integer count = (Integer) iter.next(); sum = sum + count.intValue(); } return new Integer(sum); } }); } public Integer findBooksTotalByCriteria(BookCommand bookCommand) throws DataAccessException { final Book book = bookCommand.getBook(); final Author iAuthor = bookCommand.getAuthor(); final Publisher iPublisher = book.getPublisher(); return (Integer) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { StringBuffer queryString = new StringBuffer(); boolean conditionFound = false; String title = book.getTitle(); String isbn = book.getIsbn(); String authorName = iAuthor.getAuthorName(); String publisherName = iPublisher.getPublisherName(); if(title != null){ queryString.append("lower(b.title) like :title "); conditionFound=true; } if(isbn != null){ if (conditionFound) queryString.append("and "); queryString.append("lower(b.isbn) like :isbn "); conditionFound=true; } if (authorName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(a.authorName) like :authorName "); conditionFound=true; } if (publisherName != null) { if (conditionFound) queryString.append("and "); queryString.append("lower(p.publisherName) like :publisherName "); conditionFound=true; } String fromClause = conditionFound ? "select count(distinct b.id) from Book b " + "left join b.authors a " + "left join b.publisher p where " : "select count(distinct b.id) from Book b " + "left join b.authors a " + "left join b.publisher p "; queryString.insert(0, fromClause); Query query = getSession().createQuery( queryString.toString() ); if (title != null) query.setString( "title",'%' + title.toLowerCase() + '%' ); if (isbn != null) query.setString( "isbn",'%' + isbn.toLowerCase() + '%' ); if (authorName != null) query.setString( "authorName",'%' + authorName.toLowerCase() + '%' ); if (publisherName != null) query.setString( "publisherName",'%' + publisherName.toLowerCase() + '%' ); return (Integer)query.uniqueResult(); } }); }


Reply With Quote
.....