Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package data.dao; import data.dao.parent.Dao; import data.entity.CarCompletionOption; import data.entity.CarOptionValue; import java.util.ArrayList; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author bezdatiuzer */ @Repository("carOptionValueDao") public class CarOptionValueDao extends Dao<CarOptionValue> { @Autowired private CarCompletionOptionDao carCompletionOptionDao; @Override public Class getSupportedClass() { return CarOptionValue.class; } public List<CarCompletionOption> getUniqueCCOs() { String hql = "select cov from CarOptionValue cov group by cov.CCO"; Query query = currentSession().createQuery(hql); List<CarOptionValue> covs = query.list(); List<CarCompletionOption> ccos = new ArrayList(); for (CarOptionValue cov : covs) { ccos.add(cov.getCCO()); } return ccos; } public List<CarOptionValue> getUniqueOptionNames(Long ccoId) throws Exception { if (ccoId == null) { throw new Exception("ccoId is null!"); } List<CarOptionValue> res = new ArrayList(); if (!ccoId.equals((long) 0)) { CarCompletionOption cco = carCompletionOptionDao.find(ccoId); if (cco != null) { String hql = "select distinct cov from CarOptionValue cov where cov.CCO.ccoId=:ccoId group by cov.description order by cov.description"; Query query = currentSession().createQuery(hql).setParameter("ccoId", ccoId); res = query.list(); } } else if (ccoId.equals((long) 0)) { String hql = "select cov from CarOptionValue cov group by cov.description,cov.CCO.ccoId order by cov.CCO.uid,cov.CCO.title,cov.description"; Query query = currentSession().createQuery(hql); res = query.list(); } //throw new Exception("size="+res.size()+"; "); return res; } public List<CarOptionValue> findByDescAndCco(Long covId) { List<CarOptionValue> res = new ArrayList(); //if(cov!=null&&cov.getCCO()!=null&&cov.getDescription()!=null){ //Long ccoId=cov.getCCO().getCcoId(); /*CarCompletionOption cco = carCompletionOptionDao.find(ccoId); Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); cr.add(Restrictions.eq("description",desc)); cr.add(Restrictions.eq("CCO",cco)); return cr.list();*/ CarOptionValue cov = find(covId); //String sql = "select "; String hql = "select cov from CarOptionValue cov where cov.CCO=:CCO and cov.description=:desc"; Query query = currentSession().createQuery(hql).setEntity("CCO", cov.getCCO()).setParameter("desc", cov.getDescription()); //Query query = currentSession().createQuery(hql).setParameter("CCOId", cov.getCCO().getId()).setParameter("desc", cov.getDescription()); res = query.list(); return res; } }