public pageBean seachProductByBrank(int pageSize, int currentLPage,
String product_Brank, String product_itemName) {
final String hql = "from " + OgrilProducts.class.getName()
+ " where product_Brank='" + product_Brank
+ "' and product_itemName='" + product_itemName
+ "' order by product_registerDate desc";
int allRow = this.getAllRowCount(hql);// 总记录数
int totalPage = pageBean.countTotalpage(pageSize, allRow);// 总页数
final int offset = pageBean.countOffset(pageSize, currentLPage);// 当前页开始记录
final int length = pageSize;
final int currentPage = pageBean.countCurrentPage(currentLPage);
List list = this.queryForPage(hql, offset, length);// 记录
// 把分页信息保存到Bean中
pageBean pagebean = new pageBean();
pagebean.setPageSize(pageSize);
pagebean.setCurrentPage(currentPage);
pagebean.setAllRow(allRow);
pagebean.setTotalPage(totalPage);
pagebean.setList(list);
return pagebean;
}
以上是查询分页的具体代码。 HQL 如何消除重复项?

解决方案 »

  1.   


    你是指有重复数据重复么?那么distinc下。或者使用Set集合
      

  2.   

     List list = this.queryForPage(hql, offset, length);// 记录
    List<OgrilProducts> list = this.queryForPage(hql, offset, length)
    Set<OgrilProducts> sets = new HashSet<OgrilProducts>();
    for(OgrilProducts op : list){
        sets.add(op);
    }
      

  3.   

    用DISTINCT可以解決大部份問題String hql = "select distinct(obj.number),obj.a,obj.b from Object obj where ...";如果你要按NUMBER分組顯示的話就用GROUPBYString hql = "select obj from Object obj where ... group by obj.number";但是簡單的GROUPBY可能滿足不了你需求,還可以再CONCAT其他條件,不過看具體需求了
    還有一個非DATABASE的解決方案那就是如樓上老兄所止的SET
      

  4.   

    "select distinct(obj) from Object obj group by obj.product_number having count(obj.product_number) > 0"
      

  5.   


    用set是由前提的。那就算对象所存数据一样。既然你无法使用,证明你只是想让重复的货号取一个。那你就使用distinct吧。将你的HQL改动下。 select id, distinct(商品货号),商品品牌from OgrilProducts 不过这时候查出来的List是 List<Object[]>  
     
    后面的自己写写看了,思路就这样。 
      

  6.   

    先equals(),hashcode()处理后,再用Set集合进行处理可以的。
      

  7.   

     谢谢各位的关注和回复 上午想到了一个比较简单的办法 用HQL语句解决的。代码如下:/**
     * 功能:根据品牌查询
     * 
     * @param pageSize
     *            每页大小
     * @param currentLPagem
     *            当前第几页
     * @param productBlack
     *            产品品牌
     * @param productBlackItem
     *            产品分类
     * @param productSize
     *            产品尺码
     * @param keyWord
     *            搜索关键字
     * @return
     */
    public pageBean seachProductByBrank(int pageSize, int currentLPage,
    String product_Brank, String product_itemName) {
    // final String hql = "from " + OgrilProducts.class.getName()
    // + " where product_Brank='" + product_Brank
    // + "' and product_itemName='" + product_itemName
    // + "' order by product_registerDate desc";
    final String hql = " from "
    + OgrilProducts.class.getName()
    + " as product where  product.productItemName='"
    + product_itemName
    + "' and productBrank='"
    + product_Brank
    + "' and not exists( from "
    + OgrilProducts.class.getName()
    + " where productItemName='"
    + product_itemName
    + "' and productBrank='"
    + product_Brank
    + "' and productItemNumber=product.productItemNumber and productId<product.productId )";
    int allRow = this.getAllRowCount(hql);// 总记录数
    int totalPage = pageBean.countTotalpage(pageSize, allRow);// 总页数
    final int offset = pageBean.countOffset(pageSize, currentLPage);// 当前页开始记录
    final int length = pageSize;
    final int currentPage = pageBean.countCurrentPage(currentLPage);
    List list = this.queryForPage(hql, offset, length);// 记录
    // 把分页信息保存到Bean中
    pageBean pagebean = new pageBean();
    pagebean.setPageSize(pageSize);
    pagebean.setCurrentPage(currentPage);
    pagebean.setAllRow(allRow);
    pagebean.setTotalPage(totalPage);
    pagebean.setList(list);
    return pagebean;
    }
    注释的是以前分页的方法 改动了一下 问题解决了 
      现在结贴.....