private void createAssociationPathCriteriaMap() { Iterator iter = rootCriteria.iterateSubcriteria(); while ( iter.hasNext() ) { CriteriaImpl.Subcriteria crit = ( CriteriaImpl.Subcriteria ) iter.next(); String wholeAssociationPath = getWholeAssociationPath( crit ); Object old = associationPathCriteriaMap.put( wholeAssociationPath, crit ); if ( old != null ) { throw new QueryException( "duplicate association path: " + wholeAssociationPath ); } int joinType = crit.getJoinType(); old = associationPathJoinTypesMap.put( wholeAssociationPath, new Integer( joinType ) ); if ( old != null ) { // TODO : not so sure this is needed... throw new QueryException( "duplicate association path: " + wholeAssociationPath ); } } }
@SuppressWarnings("rawtypes") public int queryCount(DetachedCriteria detachedCriteria, String dataSourceName) { Session session = this.getSessionFactory(dataSourceName).openSession(); try { org.hibernate.Criteria criteria = detachedCriteria.getExecutableCriteria(session); try { Field field = CriteriaImpl.class.getDeclaredField("orderEntries"); field.setAccessible(true); field.set(criteria, new ArrayList()); } catch (Exception e) { e.printStackTrace(); } int totalCount = 0; Object totalObject = criteria.setProjection(Projections.rowCount()).uniqueResult(); if (totalObject instanceof Integer) { totalCount = (Integer) totalObject; } else if (totalObject instanceof Long) { totalCount = ((Long) totalObject).intValue(); } else if (totalObject != null) { throw new RuntimeException("Can not convert [" + totalObject + "] to a int value when query entity total count!"); } return totalCount; } finally { session.flush(); session.close(); } }
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(); }
public CriteriaJoinWalker( final OuterJoinLoadable persister, final CriteriaQueryTranslator translator, final SessionFactoryImplementor factory, final CriteriaImpl criteria, final String rootEntityName, final Map enabledFilters) throws HibernateException { super(persister, factory, enabledFilters); this.translator = translator; querySpaces = translator.getQuerySpaces(); if ( translator.hasProjection() ) { resultTypes = translator.getProjectedTypes(); initProjection( translator.getSelect(), translator.getWhereCondition(), translator.getOrderBy(), translator.getGroupBy(), LockMode.NONE ); } else { resultTypes = new Type[] { TypeFactory.manyToOne( persister.getEntityName() ) }; initAll( translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE ); } userAliasList.add( criteria.getAlias() ); //root entity comes *last* userAliases = ArrayHelper.toStringArray(userAliasList); }
public CriteriaLoader( final OuterJoinLoadable persister, final SessionFactoryImplementor factory, final CriteriaImpl criteria, final String rootEntityName, final Map enabledFilters) throws HibernateException { super(factory, enabledFilters); translator = new CriteriaQueryTranslator( factory, criteria, rootEntityName, CriteriaQueryTranslator.ROOT_SQL_ALIAS ); querySpaces = translator.getQuerySpaces(); CriteriaJoinWalker walker = new CriteriaJoinWalker( persister, translator, factory, criteria, rootEntityName, enabledFilters ); initFromWalker(walker); userAliases = walker.getUserAliases(); resultTypes = walker.getResultTypes(); postInstantiate(); }
public CriteriaQueryTranslator( final SessionFactoryImplementor factory, final CriteriaImpl criteria, final String rootEntityName, final String rootSQLAlias, CriteriaQuery outerQuery) throws HibernateException { this( factory, criteria, rootEntityName, rootSQLAlias ); outerQueryTranslator = outerQuery; }
public CriteriaQueryTranslator( final SessionFactoryImplementor factory, final CriteriaImpl criteria, final String rootEntityName, final String rootSQLAlias) throws HibernateException { this.rootCriteria = criteria; this.rootEntityName = rootEntityName; this.sessionFactory = factory; this.rootSQLAlias = rootSQLAlias; createAliasCriteriaMap(); createAssociationPathCriteriaMap(); createCriteriaEntityNameMap(); createCriteriaSQLAliasMap(); }
private String getWholeAssociationPath(CriteriaImpl.Subcriteria subcriteria) { String path = subcriteria.getPath(); // some messy, complex stuff here, since createCriteria() can take an // aliased path, or a path rooted at the creating criteria instance Criteria parent = null; if ( path.indexOf( '.' ) > 0 ) { // if it is a compound path String testAlias = StringHelper.root( path ); if ( !testAlias.equals( subcriteria.getAlias() ) ) { // and the qualifier is not the alias of this criteria // -> check to see if we belong to some criteria other // than the one that created us parent = ( Criteria ) aliasCriteriaMap.get( testAlias ); } } if ( parent == null ) { // otherwise assume the parent is the the criteria that created us parent = subcriteria.getParent(); } else { path = StringHelper.unroot( path ); } if ( parent.equals( rootCriteria ) ) { // if its the root criteria, we are done return path; } else { // otherwise, recurse return getWholeAssociationPath( ( CriteriaImpl.Subcriteria ) parent ) + '.' + path; } }
public String getWhereCondition() { StringBuffer condition = new StringBuffer( 30 ); Iterator criterionIterator = rootCriteria.iterateExpressionEntries(); while ( criterionIterator.hasNext() ) { CriteriaImpl.CriterionEntry entry = ( CriteriaImpl.CriterionEntry ) criterionIterator.next(); String sqlString = entry.getCriterion().toSqlString( entry.getCriteria(), this ); condition.append( sqlString ); if ( criterionIterator.hasNext() ) { condition.append( " and " ); } } return condition.toString(); }
public String getOrderBy() { StringBuffer orderBy = new StringBuffer( 30 ); Iterator criterionIterator = rootCriteria.iterateOrderings(); while ( criterionIterator.hasNext() ) { CriteriaImpl.OrderEntry oe = ( CriteriaImpl.OrderEntry ) criterionIterator.next(); orderBy.append( oe.getOrder().toSqlString( oe.getCriteria(), this ) ); if ( criterionIterator.hasNext() ) { orderBy.append( ", " ); } } return orderBy.toString(); }
/** * Get Page, apply filter if any. * If Filter is a entity model, use Example to create a criteria. * else enable filter by name on session. * @param page with page definitions * @return page of results */ @SuppressWarnings({ "unchecked", "rawtypes" }) public <K> Page<K> getPage(Page<K> page) { List data = null; // try named query Query query = getQuery(page); if (query != null) { data = query.list(); } else { // try filter, example and criteria builders Criteria criteria = getCriteria(page); ResultTransformer rt = ((CriteriaImpl) criteria).getResultTransformer(); criteria.setProjection(Projections.rowCount()); page.setCount(((Long) criteria.uniqueResult()).intValue()); // reset criteria criteria.setProjection(null); criteria.setResultTransformer(rt); // set start index and page size criteria.setFirstResult(page.getStartIndex()) .setMaxResults(page.getPageSize()); applyOrder(page, criteria); // run it criteria.setCacheable(cachePageQueries); data = criteria.list(); } page.setData(data); return page; }
/** * Return a existing alias for propertyPath on Criteria or null if none * @param criteria Hibernate Criteria * @param propertyPath the property path * @return alias or null if none */ public static String findAliasForPropertyPath(Criteria criteria, String propertyPath) { CriteriaImpl c = (CriteriaImpl) criteria; Iterator iter = c.iterateSubcriteria(); while (iter.hasNext()) { Subcriteria subCriteria = (Subcriteria) iter.next(); if (propertyPath.equals(subCriteria.getPath())); return subCriteria.getAlias(); } // not found return null; }
protected DetachedCriteria(String entityName) { impl = new CriteriaImpl(entityName, null); criteria = impl; }
protected DetachedCriteria(String entityName, String alias) { impl = new CriteriaImpl(entityName, alias, null); criteria = impl; }
protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) { this.impl = impl; this.criteria = criteria; }
CriteriaImpl getCriteriaImpl() { return impl; }
public CriteriaImpl getRootCriteria() { return rootCriteria; }
public JazzDetachedCriteria(CriteriaImpl impl, Criteria criteria) { super(impl, criteria); }
protected OnmsDetachedCriteria(String entityName) { this(new CriteriaImpl(entityName, null)); }
protected OnmsDetachedCriteria(String entityName, String alias) { this(new CriteriaImpl(entityName, alias, null)); }
protected OnmsDetachedCriteria(CriteriaImpl impl) { super(impl, impl); m_impl = impl; }
protected OnmsDetachedCriteria(CriteriaImpl impl, Criteria criteria) { super(impl, criteria); m_impl = impl; }
/** * @param request * @return * @see edu.utah.further.core.chain.AbstractRequestHandler#process(edu.utah.further.core.api.chain.ChainRequest) * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-817 */ @Override public boolean process(final ChainRequest request) { final HibernateExecReq executionReq = new HibernateExecReq(request); // Validate required input final GenericCriteria hibernateCriteria = executionReq.getResult(); notNull(hibernateCriteria, "Expected Hibernate criteria"); final Class<? extends PersistentEntity<?>> domainClass = executionReq .getRootEntity(); final Class<? extends PersistentEntity<?>> entityClass = dao .getEntityClass(domainClass); notNull(entityClass, "Expected root entity class"); final SessionFactory sessionFactory = executionReq.getSessionFactory(); notNull(sessionFactory, "Expected SessionFactory"); final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass); final String identifierName = classMetadata.getIdentifierPropertyName(); final Type identifierType = classMetadata.getIdentifierType(); // A hack to obtain projections out of the critieria by casting to the Hibernate // implementation. TODO: improve adapter to do that via interface access final ProjectionList projectionList = Projections.projectionList(); final Projection existingProjection = ((CriteriaImpl) hibernateCriteria .getHibernateCriteria()).getProjection(); if (existingProjection != null && !overrideExistingProjection) { return false; } if (identifierType.isComponentType()) { final ComponentType componentType = (ComponentType) identifierType; final String[] idPropertyNames = componentType.getPropertyNames(); // Add distinct to the first property projectionList.add( Projections.distinct(Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[0])), idPropertyNames[0]); // Add the remaining properties to the projection list for (int i = 1; i < idPropertyNames.length; i++) { projectionList.add( Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[i]), idPropertyNames[i]); } hibernateCriteria.setProjection(projectionList); hibernateCriteria.setResultTransformer(new AliasToBeanResultTransformer( ReflectionUtils.findField(entityClass, identifierName).getType())); } else { // 'this' required to avoid HHH-817 projectionList.add(Projections.distinct(Property.forName(THIS_CONTEXT + identifierName))); hibernateCriteria.setProjection(projectionList); } executionReq.setResult(hibernateCriteria); return false; }
/** * Constructor. * * @param impl CriteriaImpl * @param criteria Criteria */ public EphesoftCriteria(CriteriaImpl impl, Criteria criteria) { super(impl, criteria); }
/** * Execute a criteria query */ public ScrollableResults scroll(CriteriaImpl criteria, ScrollMode scrollMode);
/** * Execute a criteria query */ public List list(CriteriaImpl criteria);
/** * @param impl * @param criteria */ public JazzOMRDetachedCriteria(CriteriaImpl impl, Criteria criteria) { super(impl, criteria); }
/** * @param impl * @param criteria */ public JazzOMRDetachedCriteria(CriteriaImpl impl, Criteria criteria) { super(impl,criteria); }