Manager-service-之Hibernate几种查询方式,在需要情况下配合使用。
如果要用到:普通sql语句
public String getAllSubDepId(Long depId){
String sql="select t.departmentid as id from department t start with t.departmentid="+depId+" connect by prior t.departmentid=t.fatherdepartmentid";
Session session = dao.getSessionFactory().getCurrentSession();
List<Long> list=session.createSQLQuery(sql).addScalar("id",Hibernate.LONG).list();
String ids="";
for(int i=0;i<list.size();i++){
ids=ids+list.get(i);
if(i<list.size()-1)
ids=ids+",";
}
return ids;
}
带分页用HQL查询时候:
public Page<IdentityInfo> getViewObjectsNew(Page<IdentityInfo> page,IdentityInfo entity) {
String hql="from IdentityInfo t where t.delFlag=0 ";
if(entity.getDepartmentEntity()!=null&&entity.getDepartmentEntity().getDepartmentId()!=null && entity.getDepartmentEntity().getDepartmentId().compareTo(0l)>0){
hql=hql+" and t.departmentEntity.id="+entity.getDepartmentEntity().getDepartmentId();
}else{
User user=SessionManager.getUserSession();
String hasAdminRole=roleManager.hasRole(user.getId(), 104l);
if(hasAdminRole.equals("YES"))
hql=hql+" ";
else{
String ids=departmentManager.getAllSubDepId(user.getDepartment().getId());
hql=hql+"and t.departmentEntity.id in("+ids+") ";
}
}
hql=hql+" order by t.priority,t.id";
return dao.find(page, hql,null);
}
用Criterion对象查询方式:
@Transactional(readOnly = true)
public List<IdentityInfo> getAllIdentityInfo(Page<IdentityInfo> page,IdentityInfo entity) {
List<Criterion> list = new ArrayList<Criterion>();
if (entity.getName() != null && entity.getName().trim().length() > 0)
list.add(Restrictions.like("name", '%' + entity.getName() + '%'));
if (entity.getDepartmentEntity() != null && entity.getDepartmentEntity().getDepartmentId() != null &&entity.getDepartmentEntity().getDepartmentId().compareTo(0l)>0)
list.add(Restrictions.eq("departmentEntity.departmentId", entity.getDepartmentEntity().getDepartmentId()));
if (list.size() > 0)
return dao.findByCriteria( list.toArray(new Criterion[list.size()]));
else
return dao.findAll();
}
用对象的第2种形式:
public Page<ResTeam> getObjects(Page<ResTeam> page, ResTeam entity) {
Criteria critera = dao.getSession().createCriteria(ResTeam.class);
if (entity.getType()!= null && !entity.getType().equals("all")) {
critera.add(Restrictions.like("type", '%' + StrUtils
.removedPercent(entity.getType().trim()) + '%'));
}
if (entity.getName() != null && entity.getName().trim().length() > 0)
critera.add(Restrictions.like("name", '%' + StrUtils
.removedPercent(entity.getName().trim()) + '%'));
if (entity.getStationInfo() != null) {
String stationName = entity.getStationInfo().getStationName();
if (stationName != null && stationName.trim().length() > 0) {
critera.createAlias("stationInfo", "st");
critera.add(Restrictions.like("st.stationName", '%' + StrUtils
.removedPercent(stationName.trim()) + '%'));
}
}
if (entity.getBaseOrg() != null && entity.getBaseOrg().getId()!=null)
{
critera.createAlias("baseOrg", "base");
critera.add(Restrictions.eq("base.id", entity.getBaseOrg().getId()));
}
critera.add(Restrictions.not(Restrictions.eq("delflag", 1)));
critera.addOrder(Order.desc("createDate"));
return dao.findByCriteria(page, critera);
}
3种方式对比:
public Page<GroupMember> getViewObjectsNew(Page<GroupMember> page,GroupMember entity) {
*//********** 这里判断多个,首先部门对象不为空,然后部门对象的ID不为空 **********//*
//法1:
if(entity.getDepartmentEntity()!=null&&entity.getDepartmentEntity().getDepartmentId()!=null && entity.getDepartmentEntity().getDepartmentId().compareTo(0l)>0){
String hql = "from GroupMember t where t.departmentEntity.departmentId=?";
return dao.find(page, hql, new Object[]{entity.getDepartmentEntity().getDepartmentId() });
}else{
return dao.findAll(page);
}
if(entity.getGpsEntity()!=null && entity.getGpsEntity().getId()!=null && entity.getGpsEntity().getId().compareTo(0l)>0){
String hql="from GroupMember m where m.gpsEntity.id=?";
return dao.find(page, hql, new Object[]{entity.getGpsEntity().getId()});
}else{
return dao.findAll(page);
}
//法2:
Criteria criteria=dao.getSession().createCriteria(GroupMember.class);
if(entity.getGpsEntity()!=null && entity.getGpsEntity().getId()!=null && entity.getGpsEntity().getId().compareTo(0l)>0){
System.out.println(entity.getGpsEntity().getId()+" ============manage id");
criteria.add(Restrictions.eq("gpsEntity.id", entity.getGpsEntity().getId()));
}
return dao.findByCriteria(page, criteria);
}
//法3:
public Page<GroupMember> getMemberByGroup(Page<GroupMember> page,Long groupId) {
String hql="from GroupMember t where t.gpsEntity.id=? and t.infoEntity.delFlag=0 order by infoEntity.departmentEntity.parent.divNum,infoEntity.departmentEntity.priority,infoEntity.priority,t.priority";
//String hql="from GroupMember t where t.gpsEntity.id=? order by t.priority";
return dao.find(page, hql, new Object[]{groupId});
}