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.Car; import data.entity.CarProperty; import data.entity.PropertyName; import java.util.ArrayList; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Property; import org.hibernate.criterion.Restrictions; import org.hibernate.transform.AliasToBeanResultTransformer; import org.hibernate.transform.Transformers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author bezdatiuzer */ @Repository("propertyNameDao") public class PropertyNameDao extends Dao<PropertyName> { @Autowired private CarPropertyDao carPropertyDao; @Override public Class getSupportedClass() { return PropertyName.class; } /*public List<PropertyName> getParamListByCarAndPropId(PropertyName pn){ List<PropertyName> result = new ArrayList(); if(pn!=null){ if(pn.getCar()!=null&&pn.getCarProperty()!=null){ Session session = currentSession(); SQLQuery query = session.createSQLQuery("SELECT * FROM property_name where car_id="+pn.getCar().getCmqId()+" and car_property_id="+pn.getCarProperty().getOldId()+";"); } } return result; }*/ public List<Long> getUniqueCarIds() { /* Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setProjection(Projections.distinct(Projections.property("car_id"))); cr.addOrder(Order.asc("car_id")); return cr.list(); */ String hql = "select distinct car from Car as car inner join car.propertyNames"; Query query = currentSession().createQuery(hql); List<Car> cars = query.list(); List<Long> ids = new ArrayList(); for (Car car : cars) { ids.add(car.getCarId()); } return ids; } public List<PropertyName> getPropertiesByCar(Long carId) { Criteria cr = currentSession().createCriteria(getSupportedClass()); if (carId != null) { cr.add(Property.forName("car_id").eq(carId)); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); } return cr.list(); } /*public List<String> getUniquePropertyNames(Long propId){ CarProperty cp = carPropertyDao.find(propId); List<String> res = new ArrayList(); if(cp!=null){ //String hql = "select distinct propertyNameValue from PropertyName as pnv"; Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); cr.setProjection(Projections.distinct(Projections.property("propertyNameValue"))); cr.add(Restrictions.eq("carProperty", cp)); cr.addOrder(Order.asc("propertyNameValue")); return cr.list(); } return res; }*/ /*public List<PropertyName> getUniquePropertyNames(Long propId) throws Exception{ CarProperty cp = carPropertyDao.find(propId); List<PropertyName> res = new ArrayList(); if(cp!=null){ String hql = "select distinct propertyNameValue from PropertyName where "; Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); cr.setProjection(Projections.distinct(Projections.property("propertyNameValue"))); //cr.add(Restrictions.eq("carProperty", cp)); //cr.addOrder(Order.asc("propertyNameValue")); //cr.setResultTransformer(new AliasToBeanResultTransformer(getSupportedClass())); /*if(!cr.list().isEmpty()){ throw new Exception(cr.list().get(0).getClass().getName()); }*/ /*return cr.list(); } return res; }*/ /*public List<PropertyName> getUniquePropertyNames(Long propId){ CarProperty cp = carPropertyDao.find(propId); List<PropertyName> res = new ArrayList(); if(cp!=null){ //String hql = "select distinct propertyNameValue from PropertyName as pnv"; Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setProjection(Projections.distinct(Projections.property("propertyNameValue"))); cr.setProjection(Projections.property("radical")); cr.setProjection(Projections.groupProperty("propertyNameValue")); cr.add(Restrictions.eq("carProperty", cp)); cr.addOrder(Order.asc("propertyNameValue")); //cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); cr.setResultTransformer(new AliasToBeanResultTransformer(getSupportedClass())); return cr.list(); } return res; }*/ public List<PropertyName> getUniquePropertyNames(Long propId) throws Exception { if (propId == null) { throw new Exception("propId is null!"); } List<PropertyName> res = new ArrayList(); if (!propId.equals((long) 0)) { CarProperty cp = carPropertyDao.find(propId); if (cp != null) { String hql = "select distinct pn from PropertyName pn where pn.carProperty.cpId=:propId group by pn.propertyNameValue order by pn.propertyNameValue"; Query query = currentSession().createQuery(hql).setParameter("propId", propId); return query.list(); } } else if (propId.equals((long) 0)) { String hql = "select distinct pn from PropertyName pn group by pn.propertyNameValue order by pn.propertyNameValue"; Query query = currentSession().createQuery(hql); return query.list(); } return res; } public List<PropertyName> findByPropertyAndPnValue(PropertyName pn) { Criteria cr = currentSession().createCriteria(getSupportedClass()); cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); cr.add(Property.forName("carProperty").eq(pn.getCarProperty())); cr.add(Property.forName("propertyNameValue").eq(pn.getPropertyNameValue())); return cr.list(); } }