org.hibernate.loader.criteria.CriteriaQueryTranslator Java Examples

The following examples show how to use org.hibernate.loader.criteria.CriteriaQueryTranslator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubqueryExpression.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
	
	final SessionImplementor session = ( (CriteriaImpl) criteria ).getSession(); //ugly!
	final SessionFactoryImplementor factory = session.getFactory();
	
	final OuterJoinLoadable persister = (OuterJoinLoadable) factory.getEntityPersister( criteriaImpl.getEntityOrClassName() );
	CriteriaQueryTranslator innerQuery = new CriteriaQueryTranslator( 
			factory, 
			criteriaImpl, 
			criteriaImpl.getEntityOrClassName(), //implicit polymorphism not supported (would need a union) 
			criteriaQuery.generateSQLAlias(),
			criteriaQuery
		);
	
	params = innerQuery.getQueryParameters(); //TODO: bad lifecycle....
	types = innerQuery.getProjectedTypes();
	
	//String filter = persister.filterFragment( innerQuery.getRootSQLALias(), session.getEnabledFilters() );
	
	String sql = new Select( factory.getDialect() )
		.setWhereClause( innerQuery.getWhereCondition() )
		.setGroupByClause( innerQuery.getGroupBy() )
		.setSelectClause( innerQuery.getSelect() )
		.setFromClause(
				persister.fromTableFragment( innerQuery.getRootSQLALias() ) +   
				persister.fromJoinFragment( innerQuery.getRootSQLALias(), true, false )
			)
		.toStatementString();
	
	final StringBuffer buf = new StringBuffer()
		.append( toLeftSqlString(criteria, criteriaQuery) );
	if (op!=null) buf.append(' ').append(op).append(' ');
	if (quantifier!=null) buf.append(quantifier).append(' ');
	return buf.append('(').append(sql).append(')')
		.toString();
}
 
Example #2
Source File: VisitScheduleItemDaoImpl.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static LinkedHashMap<String, Projection> applyExpandDateModeCriterions(org.hibernate.Criteria visitScheduleItemCriteria, Long probandId, Timestamp from, Timestamp to,
		org.hibernate.criterion.Criterion or) {
	// cartesian product <visitscheduleitems> x <start tag values> x <stop tag values>
	org.hibernate.Criteria startTagValuesCriteria = visitScheduleItemCriteria.createCriteria("startTag", CriteriaSpecification.LEFT_JOIN)
			.createCriteria("tagValues", "startTagValues", CriteriaSpecification.LEFT_JOIN);
	org.hibernate.Criteria startTagValuesValueCriteria = startTagValuesCriteria.createCriteria("value",
			CriteriaSpecification.LEFT_JOIN);
	org.hibernate.Criteria stopTagValuesCriteria = visitScheduleItemCriteria.createCriteria("stopTag", CriteriaSpecification.LEFT_JOIN).createCriteria("tagValues",
			"stopTagValues", CriteriaSpecification.LEFT_JOIN);
	org.hibernate.Criteria stopTagValuesValueCriteria = stopTagValuesCriteria.createCriteria("value", CriteriaSpecification.LEFT_JOIN);
	// from the cross product, remove those with start+stop values of different probands. also include rows without existing stop tag values
	visitScheduleItemCriteria.add(CriteriaUtil.applyOr(
			Restrictions.or(Restrictions.eqProperty("startTagValues.listEntry.id", "stopTagValues.listEntry.id"), Restrictions.isNull("stopTagValues.listEntry.id")), or));
	// narrow to particular proband, if given
	org.hibernate.Criteria startTagValuesListEntryCriteria = startTagValuesCriteria.createCriteria("listEntry", "startTagValuesListEntry", CriteriaSpecification.LEFT_JOIN);
	if (probandId != null) {
		startTagValuesListEntryCriteria.add(CriteriaUtil.applyOr(Restrictions.or(Restrictions.isNull("proband.id"), Restrictions.eq("proband.id", probandId.longValue())), or));
	}
	// only rows with proband group matching the group of the visitschelute item (or those with no group)
	visitScheduleItemCriteria
			.add(CriteriaUtil.applyOr(Restrictions.or(Restrictions.isNull("startTagValuesListEntry.id"),
					Restrictions.or(Restrictions.eqProperty("startTagValuesListEntry.group.id", "group.id"), Restrictions.isNull("group.id"))), or));
	CriteriaQueryTranslator translator = CriteriaUtil.getCriteriaQueryTranslator(visitScheduleItemCriteria);
	// prepare sql fragments:
	String offsetSql = translator.getColumn(visitScheduleItemCriteria, "offsetSeconds");
	String durationSql = translator.getColumn(visitScheduleItemCriteria, "duration");
	String tagStartSql = translator.getColumn(startTagValuesValueCriteria, "timestampValue");
	String tagStopSql = translator.getColumn(stopTagValuesValueCriteria, "timestampValue");
	String tagStartOffsetSql = MessageFormat.format(Settings.getString(SettingCodes.SQL_ADD_SECONDS_TERM, Bundle.SETTINGS, null),
			tagStartSql, offsetSql);
	String tagStopOffsetSql = MessageFormat.format(Settings.getString(SettingCodes.SQL_ADD_SECONDS_TERM, Bundle.SETTINGS, null),
			tagStopSql, offsetSql);
	String durationStopOffsetSql = MessageFormat.format(Settings.getString(SettingCodes.SQL_ADD_SECONDS_TERM, Bundle.SETTINGS, null),
			tagStartOffsetSql, durationSql);
	String probandIdSql = translator.getColumn(startTagValuesListEntryCriteria, "proband.id");
	// date filtering
	Junction junction = Restrictions.disjunction();
	if (or != null) {
		junction.add(or);
	}
	if (from != null || to != null) {
		junction.add(Restrictions.and(Restrictions.eq("mode", VisitScheduleDateMode.STATIC), CriteriaUtil.getClosedIntervalCriterion(from, to, null)));
		junction.add(Restrictions.and(Restrictions.eq("mode", VisitScheduleDateMode.TAGS),
				Restrictions.and(Restrictions.sqlRestriction("(" + tagStartSql + ") < (" + tagStopSql + ")"),
						CriteriaUtil.getClosedIntervalCriterion(from, to, null, tagStartOffsetSql, tagStopOffsetSql))));
		junction.add(
				Restrictions.and(Restrictions.eq("mode", VisitScheduleDateMode.TAG_DURATION), Restrictions.and(Restrictions.sqlRestriction("(" + tagStartSql + ") is not null"),
						CriteriaUtil.getClosedIntervalCriterion(from, to, null, tagStartOffsetSql, durationStopOffsetSql))));
		visitScheduleItemCriteria.add(junction);
	} else {
		junction.add(Restrictions.eq("mode", VisitScheduleDateMode.STATIC));
		junction.add(Restrictions.and(Restrictions.eq("mode", VisitScheduleDateMode.TAGS), Restrictions.sqlRestriction("(" + tagStartSql + ") < (" + tagStopSql + ")")));
		junction.add(Restrictions.and(Restrictions.eq("mode", VisitScheduleDateMode.TAG_DURATION), Restrictions.sqlRestriction("(" + tagStartSql + ") is not null")));
	}
	visitScheduleItemCriteria.add(junction); //no stales any more
	LinkedHashMap<String, Projection> sqlColumns = new LinkedHashMap<String, Projection>();
	sqlColumns.put("tagStart", new SQLProjection(
			tagStartOffsetSql + " as tagStart",
			new String[] { "tagStart" },
			new org.hibernate.type.Type[] { Hibernate.TIMESTAMP },
			tagStartOffsetSql));
	sqlColumns.put("tagStop", new SQLProjection(
			tagStopOffsetSql + " as tagStop",
			new String[] { "tagStop" },
			new org.hibernate.type.Type[] { Hibernate.TIMESTAMP },
			tagStopOffsetSql));
	sqlColumns.put("durationStop", new SQLProjection(
			durationStopOffsetSql + " as durationStop",
			new String[] { "durationStop" },
			new org.hibernate.type.Type[] { Hibernate.TIMESTAMP },
			durationStopOffsetSql));
	sqlColumns.put("probandId", new SQLProjection(
			probandIdSql + " as probandId",
			new String[] { "probandId" },
			new org.hibernate.type.Type[] { Hibernate.LONG },
			probandIdSql));
	return sqlColumns;
}