当前位置: 代码迷 >> J2EE >> List集合中包含雷同對象,結果集造成後者值覆蓋前者對象
  详细解决方案

List集合中包含雷同對象,結果集造成後者值覆蓋前者對象

热度:68   发布时间:2016-04-21 21:42:50.0
List集合中包含相同對象,結果集造成後者值覆蓋前者對象

package com.withub.admgr.dao.xwxc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.withub.admgr.core.dao.HibernateDao;
import com.withub.admgr.core.orm.query.Page;
import com.withub.admgr.entities.xwxc.XwxcAdoptDetall;
import com.withub.admgr.entities.xwxc.XwxcMagArticle;
import com.withub.admgr.entities.xwxc.XwxcSearchInfo;

@Component
public class XwxcDao extends HibernateDao<XwxcMagArticle, Integer>{
public Page<XwxcMagArticle> getList(Page<XwxcMagArticle> page,XwxcSearchInfo search){

String hql = "from XwxcMagArticle dma,XwxcAdoptDetall dad where dad.aid=dma.id";
Map<String,Object> map = new HashMap<String, Object>();


if(search.getAuthor()!=null && !"".equals(search.getAuthor())){
hql+=" and dma.author like :author";
map.put("author", "%"+search.getAuthor()+"%");
}

if(search.getTjr()!=null && !"".equals(search.getTjr())){
hql+=" and dma.tjr like :tjr";
map.put("tjr", "%"+search.getTjr()+"%");
}

if(search.getTjr2()!=null && !"".equals(search.getTjr2())){
hql+=" and dma.tjr2 like :tjr2";
map.put("tjr2", "%"+search.getTjr2()+"%");
}

if(search.getTitle()!=null && !"".equals(search.getTitle())){
hql+=" and dma.title like :title";
map.put("title", "%"+search.getTitle()+"%");
}

if(search.getBegintime()!=null && !"".equals(search.getBegintime())){
hql+=" and dma.sendtime >=:begintime";
map.put("begintime", search.getBegintime());
}

if(search.getEndtime()!=null && !"".equals(search.getEndtime())){
hql+=" and dma.sendtime <=:endtime";
map.put("endtime", search.getEndtime());
}
if(search.getStyleid()!=null){
hql+=" and dma.styleid =:styleid";
map.put("styleid", search.getStyleid());
}
if(search.getImportantinfo()!=null){
hql+=" and dma.importantinfo =:importantinfo";
map.put("importantinfo", search.getImportantinfo());
}
if(search.getTimeliness()!=null){
hql+=" and dma.timeliness =:timeliness";
map.put("timeliness", search.getTimeliness());
}
if(search.getCybegintime()!=null && !"".equals(search.getCybegintime())){
hql+=" and dad.adopttime >=:cybegintime";
map.put("cybegintime", search.getCybegintime());
}
if(search.getCyendtime()!=null && !"".equals(search.getCyendtime())){
hql+=" and dad.adopttime <=:cyendtime";
map.put("cyendtime", search.getCyendtime());
}
long count = countHqlResult(hql, map);
page.setTotalCount(count);
List list = createQuery(hql,map).setFirstResult((page.getPageNo()-1)*page.getPageSize()).setMaxResults(page.getPageSize()).list();
List<XwxcMagArticle> dlist = new ArrayList<XwxcMagArticle>();
for (int i = 0; i < list.size(); i++) {
Object[] object = (Object[]) list.get(i);

XwxcMagArticle dma= (XwxcMagArticle) object[0];
XwxcAdoptDetall dad= (XwxcAdoptDetall) object[1];
dma.setDad(dad);
dlist.add(dma);
}
page.setResult(dlist);
return page;
}

public Long countHqlResult(String hql){
return  (Long) createQuery("select count(1) "+hql).uniqueResult();
}

public List<XwxcMagArticle> getByIds(Integer[] ids){
List<XwxcMagArticle> list = getSession().createQuery("select bean from XwxcMagArticle bean where bean.id in (:ids)").setParameterList("ids", ids).list();
return list;
}

public boolean inner_bsxx(Integer[] ids) {
Integer result  = getSession().createQuery("update XwxcMagArticle set state=1 where  id in (:ids)").setParameterList("ids", ids).executeUpdate();
System.out.println("result="+result);
if(result>0){
return true;
}
return false;

}


------解决方案--------------------
引用:
找到解決辦法了!!!修改80-83行

XwxcMagArticle dma= (XwxcMagArticle) object[0];
XwxcAdoptDetall dad= (XwxcAdoptDetall) object[1];
XwxcMagArticle dma2 = new XwxcMagArticle();
  相关解决方案