首页 新闻 会员 周边 捐助

请教一下hibernate执行sql的问题

0
[已解决问题] 解决于 2016-07-27 10:25

对hibernate不太熟悉,不会转换为原生sql,代码如下,我要把sql改为select u.loginname,u.username from Organization a,
Sam_Taxempcode u where a.DistrictNumber=u.district_id 要怎么做

public PagingResult<UserInfo> finduser(UserCond qry, PagingParam pp) {

        logger.debug("call findusers()");
        String loginname = null;
        String username=null;
        String district="";
        if ( qry != null ){
            loginname = qry.getLoginname().trim();
            username=qry.getUsername().trim();
            district=String.valueOf(qry.getDistrict());
        }
        
        if ( pp == null )
            pp = new PagingParam();
        
        int end = pp.getStart() + pp.getLimit();
        
        List params = new ArrayList();
        
        String orderBy = " order by u.loginname";
        
        
        String sql = "select new cn.net.tongfang.framework.security.bo.UserInfo(u) from SamTaxempcode u ";
        StringBuffer hql = new StringBuffer(sql);
        StringBuffer cond = new StringBuffer();
        if ( loginname != null  && ! loginname.trim().equals("")) {
            cond.append( " and u.loginname like ? " );
            params.add("%" + loginname.trim() + "%");
        }
        if ( username !=null && ! username.trim().equals("")){
            cond.append(" and u.username like ? ");
            params.add("%" + username.trim() + "%");
            
        }
        
        if(district !=null && ! district.trim().equals("")){
            cond.append(" and a.id= ? ");
            params.add(district);
        }
        
        if ( params.size() > 0 ) {
            hql.append(" where 1=1 ").append( cond );
            //hql.append(" where 1=1 a.DistrictNumber=u.district_id ").append( cond );
        }
        hql.append( orderBy );
        
        
        List list;
        if ( params.size() > 0 ) {
            list = getHibernateTemplate().find(hql.toString(), params.toArray());
        } else {
            list = 
                    getHibernateTemplate().find(hql.toString());
        }
        
        end = (end < list.size()) ? end : list.size();
        List<UserInfo> result = list.subList(pp.getStart(), end);
        
        return new PagingResult<UserInfo>(list.size(), result);
    
    }
爱吃鱼的喵的主页 爱吃鱼的喵 | 初学一级 | 园豆:117
提问于:2016-05-04 16:00
< >
分享
最佳答案
0

UserInfo里面加一个构造方法:

public UserInfo(String loginname,String username){
  //省略      
}

String sql = "select new cn.net.tongfang.framework.security.bo.UserInfo(u) from SamTaxempcode u ";

改为

  String sql = "select new cn.net.tongfang.framework.security.bo.UserInfo(loginname,username) from SamTaxempcode u ";

试试

奖励园豆:5
彩虹色键盘 | 菜鸟二级 |园豆:207 | 2016-05-11 14:16

已经解决谢谢

爱吃鱼的喵 | 园豆:117 (初学一级) | 2016-07-27 10:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册