List of usage examples for org.hibernate.criterion DetachedCriteria createAlias
public DetachedCriteria createAlias(String associationPath, String alias)
From source file:org.encuestame.persistence.dao.imp.TweetPollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<TweetPollSwitch> getListAnswersByTweetPollAndDateRange(final TweetPoll tweetPoll) { final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPollSwitch.class); criteria.createAlias("tweetPoll", "tweetPoll"); criteria.add(Restrictions.eq("tweetPoll", tweetPoll)); return (List<TweetPollSwitch>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.encuestame.persistence.dao.imp.TweetPollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<TweetPoll> advancedSearch(final Boolean isPublished, final Boolean isComplete, final Boolean favourites, final Boolean scheduled, final Account user, final Integer start, final Integer max, final Integer period, final String keyword) { final SearchPeriods searchPeriods = SearchPeriods.getPeriodString(period.toString()); final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPoll.class); criteria.createAlias("question", "question"); criteria.add(Restrictions.like("question.question", keyword, MatchMode.ANYWHERE)); criteria.add(Restrictions.eq("completed", isComplete)); criteria.add(Restrictions.eq("scheduleTweetPoll", scheduled)); criteria.add(Restrictions.eq("publishTweetPoll", isPublished)); criteria.add(Restrictions.eq("favourites", favourites)); criteria.add(Restrictions.eq("tweetOwner", user)); calculateSearchPeriodsDates(searchPeriods, criteria, "createDate"); return (List<TweetPoll>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.faster.orm.criteria.CriteriaRender.java
License:Open Source License
/** * ?ID/* w w w .j av a 2 s . co m*/ * * @param dc * ? * @param subObject * ??? : vendor * @param subObjectId * ?ID */ public static void renderSubObjectId(DetachedCriteria dc, String subObject, Object subObjectId) { if (subObjectId == null) { return; } dc.createAlias(subObject, subObject); dc.add(Restrictions.eq(subObject + ".id", subObjectId)); }
From source file:org.faster.orm.criteria.CriteriaRender.java
License:Open Source License
/** * ?//ww w. j av a 2 s . com * * @param dc * * @param subObjectField * ?vendor.name * @param value * ? */ public static void renderSubObjectFieldEq(DetachedCriteria dc, String subObjectField, Object value) { if (value == null) { return; } String[] ss = subObjectField.split("\\."); dc.createAlias(ss[0], ss[0]); dc.add(Restrictions.eq(subObjectField, value)); }
From source file:org.faster.orm.criteria.CriteriaRender.java
License:Open Source License
/** * ?// ww w . j av a 2 s .c o m * * @param dc * * @param subObjectField * ?vendor.name * @param value * ? */ public static void renderSubObjectFieldLike(DetachedCriteria dc, String subObjectField, String value) { if (isBlank(value)) { return; } String[] ss = subObjectField.split("\\."); dc.createAlias(ss[0], ss[0]); dc.add(Restrictions.ilike(subObjectField, value.trim(), MatchMode.ANYWHERE)); }
From source file:org.faster.orm.criteria.CriteriaRender.java
License:Open Source License
/** * ?ID??/*from www . ja v a2s.co m*/ * * @param dc * ?? * @param subObjects * ??? * @param filterValues * ??? */ public static void renderSubObjectIdsByIntegerFilterValues(DetachedCriteria dc, String[] subObjects, String filterValues) { if (subObjects == null || subObjects.length == 0 || isBlank(filterValues)) { return; } Integer[] array = Strings.toIntegerArray(filterValues.trim(), ","); for (String fieldName : subObjects) { dc.createAlias(fieldName, fieldName); } if (array.length == 1) { if (subObjects.length == 1) { dc.add(Restrictions.eq(subObjects[0] + ".id", array[0])); } else { LogicalExpression le = Restrictions.or(Restrictions.eq(subObjects[0] + ".id", array[0]), Restrictions.eq(subObjects[1] + ".id", array[0])); for (int i = 2; i < subObjects.length; i++) { le = Restrictions.or(le, Restrictions.eq(subObjects[i] + ".id", array[0])); } dc.add(le); } } else { if (subObjects.length == 1) { dc.add(Restrictions.in(subObjects[0] + ".id", array)); } else { LogicalExpression le = Restrictions.or(Restrictions.in(subObjects[0] + ".id", array), Restrictions.in(subObjects[1] + ".id", array)); for (int i = 2; i < subObjects.length; i++) { le = Restrictions.or(le, Restrictions.eq(subObjects[i] + ".id", array)); } dc.add(le); } } }
From source file:org.faster.orm.criteria.GenericCriteria.java
License:Open Source License
public DetachedCriteria buildCriteria() { beforeBuildCriteria();/* ww w . j av a 2s . c o m*/ DetachedCriteria dc = DetachedCriteria.forClass(persistClass); List<Field> queryFields = QueryHelper.getQueryFields(this.getClass()); Set<String> aliases = new HashSet<String>(); // ?? for (Field f : queryFields) { try { Object obj = f.get(this); boolean ignoreNull = QueryHelper.isIgnoreNull(f); if (obj == null) { if (ignoreNull) { continue; } } String fieldName = QueryHelper.getFieldName(f); if (fieldName.contains(".")) { String[] subFieldNames = fieldName.split("\\."); String fieldPath = subFieldNames[0]; String alias = subFieldNames[0]; if (!aliases.contains(alias)) { dc.createAlias(fieldPath, alias); aliases.add(alias); } // ?alias for (int i = 1; i < subFieldNames.length - 1; i++) { fieldPath += "." + subFieldNames[i]; alias += "_" + subFieldNames[i]; if (!aliases.contains(alias)) { dc.createAlias(fieldPath, alias); aliases.add(alias); } } if (subFieldNames.length > 2) { fieldName = alias + "." + subFieldNames[subFieldNames.length - 1]; } } if (obj == null && !ignoreNull) { dc.add(Restrictions.isNull(fieldName)); continue; } String stringValue = String.valueOf(obj); boolean ignoreEmptyOrZero = QueryHelper.isIgnoreEmptyOrZero(f); if (ignoreEmptyOrZero && isBlank(stringValue)) { continue; } if (stringValue.startsWith(NULL)) { dc.add(Restrictions.isNull(fieldName)); continue; } if (stringValue.startsWith(NOT_NULL)) { dc.add(Restrictions.isNotNull(fieldName)); continue; } String delimiter = QueryHelper.getDelimiter(f); DataType dt = QueryHelper.getDataType(f); MatchMode mm = QueryHelper.getMatchMode(f); switch (dt) { case STRING: switch (mm) { case EQ: dc.add(Restrictions.eq(fieldName, stringValue)); continue; case LIKE: dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.ANYWHERE)); continue; case LIKE_START: dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.START)); continue; case LIKE_END: dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.END)); continue; case ILIKE: dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.ANYWHERE)); continue; case ILIKE_START: dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.START)); continue; case ILIKE_END: dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.END)); continue; case IN: CriteriaRender.renderFieldByStringFilterValues(dc, fieldName, stringValue, delimiter); continue; default: throw new IllegalArgumentException("Invalid MatchMode for String: " + mm); } case INTEGER: if (ignoreEmptyOrZero && stringValue.equals("0")) { continue; } Integer intValue = 0; if (mm != MatchMode.IN) { try { intValue = Integer.valueOf(stringValue); } catch (Exception e) { throw new IllegalArgumentException( stringValue + " is not valid int value and can't use MatchMode: " + mm); } } switch (mm) { case EQ: dc.add(Restrictions.eq(fieldName, intValue)); continue; case NE: dc.add(Restrictions.ne(fieldName, intValue)); continue; case IN: CriteriaRender.renderFieldByIntegerFilterValues(dc, fieldName, stringValue, delimiter); continue; case GT: dc.add(Restrictions.gt(fieldName, intValue)); continue; case GE: dc.add(Restrictions.ge(fieldName, intValue)); continue; case LT: dc.add(Restrictions.lt(fieldName, intValue)); continue; case LE: dc.add(Restrictions.le(fieldName, intValue)); continue; default: throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm); } case LONG: if (ignoreEmptyOrZero && stringValue.equals("0")) { continue; } Long longValue = 0L; if (mm != MatchMode.IN) { try { longValue = Long.valueOf(stringValue); } catch (Exception e) { throw new IllegalArgumentException( stringValue + " is not valid long value and can't use MatchMode: " + mm); } } switch (mm) { case EQ: dc.add(Restrictions.eq(fieldName, longValue)); continue; case NE: dc.add(Restrictions.ne(fieldName, longValue)); continue; case IN: CriteriaRender.renderFieldByLongFilterValues(dc, fieldName, stringValue, delimiter); continue; case GT: dc.add(Restrictions.gt(fieldName, longValue)); continue; case GE: dc.add(Restrictions.ge(fieldName, longValue)); continue; case LT: dc.add(Restrictions.lt(fieldName, longValue)); continue; case LE: dc.add(Restrictions.le(fieldName, longValue)); continue; default: throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm); } case DATE: // TODO ?????? Date date = DateTimes.parseStringToDate(stringValue); switch (mm) { case EQ: dc.add(Restrictions.eq(fieldName, date)); continue; case NE: dc.add(Restrictions.ne(fieldName, date)); continue; case GT: dc.add(Restrictions.gt(fieldName, date)); continue; case GE: dc.add(Restrictions.ge(fieldName, date)); continue; case LT: dc.add(Restrictions.lt(fieldName, date)); continue; case LE: dc.add(Restrictions.le(fieldName, date)); continue; default: throw new IllegalArgumentException("Invalid MatchMode for Date: " + mm); } case BOOLEAN: Boolean bool = BooleanUtils.toBooleanObject(stringValue); switch (mm) { case EQ: dc.add(Restrictions.eq(fieldName, bool)); continue; case NE: dc.add(Restrictions.ne(fieldName, bool)); continue; default: throw new IllegalArgumentException("Invalid MatchMode for Boolean: " + mm); } case LatLong: LatLngBound b = new LatLngBound(stringValue); if (b.isValid()) { String minLatitude = b.getMinLatitude() == null ? "0.0" : b.getMinLatitude().toString(); String maxLatitude = b.getMaxLatitude() == null ? "0.0" : b.getMaxLatitude().toString(); String minLongitude = b.getMinLongitude() == null ? "0.0" : b.getMinLongitude().toString(); String maxLongitude = b.getMaxLongitude() == null ? "0.0" : b.getMaxLongitude().toString(); if ("area".equalsIgnoreCase(fieldName)) { dc.add(Restrictions.between("latitude", minLatitude, maxLatitude)); dc.add(Restrictions.between("longitude", minLongitude, maxLongitude)); } else { if (fieldName.contains(".")) { String alias = fieldName.replace(".", "_"); if (!aliases.contains(alias)) { dc.createAlias(fieldName, alias); aliases.add(alias); } fieldName = alias; } else { if (!aliases.contains(fieldName)) { dc.createAlias(fieldName, fieldName); aliases.add(fieldName); } } dc.add(Restrictions.between(fieldName + ".latitude", minLatitude, maxLatitude)); dc.add(Restrictions.between(fieldName + ".longitude", minLongitude, maxLongitude)); } } continue; default: throw new IllegalArgumentException("Unsupported DataType: " + dt); } } catch (Exception e) { throw new IllegalArgumentException(e); } } renderCriteria(dc); if (isNotBlank(sort)) { String[] fields = sort.split("-"); if (fields.length < 2 || "desc".equalsIgnoreCase(fields[1])) { dc.addOrder(Order.desc(fields[0])); } else { dc.addOrder(Order.asc(fields[0])); } } return dc; }
From source file:org.gaixie.micrite.car.dao.hibernate.CarfileDAOImpl.java
License:Open Source License
public int findByCreateDateSpacingCount(int carType) { DetachedCriteria criteria = DetachedCriteria.forClass(Carfile.class); criteria.add(Expression.eq("status", ICarfileService.STATUS_NORMAL)); criteria.createAlias("skillRank", "cs"); if (0 != carType) { criteria.add(Expression.eq("cs.id", carType)); }// w ww . j a v a2s . c o m criteria.setProjection(Projections.rowCount()); return (Integer) getHibernateTemplate().findByCriteria(criteria).get(0); }
From source file:org.gaixie.micrite.car.dao.hibernate.CarfileDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public List<Carfile> findByCreateDateSpacingPerPage(int start, int limit, int carType) { DetachedCriteria criteria = DetachedCriteria.forClass(Carfile.class); criteria.add(Expression.eq("status", ICarfileService.STATUS_NORMAL)); criteria.addOrder(Order.desc("editDate")); criteria.createAlias("skillRank", "cs"); if (0 != carType) { criteria.add(Expression.eq("cs.id", carType)); }/*from w w w . jav a 2s. c om*/ List<Carfile> list = getHibernateTemplate().findByCriteria(criteria, start, limit); desidedExpiredFlag(list); return list; }
From source file:org.gaixie.micrite.car.dao.hibernate.CarfileDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public List findCSGroupByTelVague(SearchBean[] queryBean) { DetachedCriteria criteria = SearchFactory.generateCriteria(Carfile.class, queryBean); criteria.add(Expression.eq("status", ICarfileService.STATUS_NORMAL)); criteria.addOrder(Order.desc("editDate")); criteria.createAlias("carType", "cs"); criteria.setProjection(Projections.projectionList().add(Projections.count("cs.name")) .add(Projections.groupProperty("cs.name"))); List<Carfile> list = getHibernateTemplate().findByCriteria(criteria); // desidedExpiredFlag(list); return list;/*from w ww.jav a 2 s . c om*/ }