hibernate 一些查询操作

来源:互联网 发布:风尚设计淘宝美工教程 编辑:程序博客网 时间:2024/05/04 22:30

查询数据得到List<Long>

List<Organization> organizationList = Lists.newArrayList();
   
CriteriaBuilder criteriaBuilder = entityManager().getCriteriaBuilder();
CriteriaQuery<Organization> query = criteriaBuilder.createQuery(Organization.class);
Root<Organization> root = query.from(Organization.class);

organizationList = entityManager().createQuery(query.select(root).where(criteriaBuilder.equal(root.get("organizationLeaderWorkNumber"), workNumber)))
                        .getResultList();

List<Long> orgList = Lists.newArrayList();
StringBuffer sbSQL = new StringBuffer(520);
sbSQL.append("select pos.org_id from HCM_OUT_POS pos,HCM_OUT_PLU plu ");
sbSQL.append("       where pos.position_id = plu.plans and plu.spernr = '000001' and pos.is_manager = 'X'");
Query querys = entityManager().createNativeQuery(sbSQL.toString());
orgList = (List<Long>)querys.getResultList();
if(orgList!=null && orgList.size()>0){//删除organizationList包含orgList 的元素对象
Iterator<Organization> iter = organizationList.iterator(); 
while(iter.hasNext()){  
Organization organization = iter.next();  
if(orgList.contains(organization.getOrganizationID())){
iter.remove();  
 }  
}  
}



查询返回条数

 Query userCounts = entityManager().createNativeQuery("select count(*) from pmp_user u1 join" +
                " pmp_user u2 on u1.SUPERVISOR_WORK_NUMBER = u2.WORK_NUMBER " +
                "and u1.WORK_NUMBER = :workNumber and u2.id = :userId");
        userCounts.setParameter("workNumber", requestUserWorkNumber);
        userCounts.setParameter("userId", currentUserId);
        try {
            Object singleResult = userCounts.getSingleResult();
            return ((BigDecimal) singleResult).longValue() > 0;
        } catch (Exception e) {
            return false;
        }





0 0
原创粉丝点击