请教一下hibernate执行sql的问题

对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);
    
    }
慕勒3428872
浏览 600回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java