List of usage examples for org.hibernate.internal CriteriaImpl getEntityOrClassName
public String getEntityOrClassName()
From source file:com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator.java
License:Apache License
/** * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade. * Method provides translation from hibernate {@link Criteria} to plain SQL string query. * * @param criteria/*ww w. j a v a 2 s.c o m*/ * @return SQL string, null if criteria parameter was null. */ public static String toSql(Criteria criteria) { if (criteria == null) { return null; } try { CriteriaImpl c; if (criteria instanceof CriteriaImpl) { c = (CriteriaImpl) criteria; } else { CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) criteria; c = (CriteriaImpl) subcriteria.getParent(); } SessionImpl s = (SessionImpl) c.getSession(); SessionFactoryImplementor factory = s.getSessionFactory(); String[] implementors = factory.getImplementors(c.getEntityOrClassName()); CriteriaLoader loader = new CriteriaLoader( (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0], s.getLoadQueryInfluencers()); Field f = OuterJoinLoader.class.getDeclaredField("sql"); f.setAccessible(true); return (String) f.get(loader); } catch (Exception ex) { throw new SystemException(ex.getMessage(), ex); } }
From source file:org.candlepin.model.DetachedCandlepinQuery.java
License:Open Source License
/** * {@inheritDoc}/* w w w. j a v a2 s .co m*/ */ @Override @SuppressWarnings({ "unchecked", "checkstyle:indentation" }) public int getRowCount() { CriteriaImpl executable = (CriteriaImpl) this.getExecutableCriteria(); // Impl note: // We're using the projection method here over using a cursor to scroll the results due to // limitations on various connectors' cursor implementations. Some don't properly support // fast-forwarding/jumping (Oracle) and others fake the whole thing by running the query // and pretending to scroll (MySQL). Until these are addressed, the hack below is going to // be far more performant and significantly safer (which makes me sad). // Remove any ordering that may be applied (since we almost certainly won't have the field // available anymore) for (Iterator iterator = executable.iterateOrderings(); iterator.hasNext();) { iterator.next(); iterator.remove(); } Projection projection = executable.getProjection(); if (projection != null && projection.isGrouped()) { // We have a projection that alters the grouping of the query. We need to rebuild the // projection such that it gets our row count and properly applies the group by // statement. // The logic for this block is largely derived from this Stack Overflow posting: // http://stackoverflow.com/ // questions/32498229/hibernate-row-count-on-criteria-with-already-set-projection // // A safer alternative may be to generate a query that uses the given criteria as a // subquery (SELECT count(*) FROM (<criteria SQL>)), but is probably less performant // than this hack. CriteriaQueryTranslator translator = new CriteriaQueryTranslator( (SessionFactoryImplementor) this.session.getSessionFactory(), executable, executable.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS); executable.setProjection( Projections.projectionList().add(Projections.rowCount()).add(Projections.sqlGroupProjection( "count(count(1))", translator.getGroupBy(), new String[] {}, new Type[] {}))); } else { executable.setProjection(Projections.rowCount()); } Long count = (Long) executable.uniqueResult(); return count != null ? count.intValue() : 0; }
From source file:org.grails.orm.hibernate.query.HibernateQuery.java
License:Apache License
@Override public Object clone() { final CriteriaImpl impl = (CriteriaImpl) criteria; final HibernateSession hibernateSession = (HibernateSession) getSession(); final GrailsHibernateTemplate hibernateTemplate = (GrailsHibernateTemplate) hibernateSession .getNativeInterface();// w ww. j a va 2 s . c om return hibernateTemplate.execute(new GrailsHibernateTemplate.HibernateCallback<Object>() { @Override public HibernateQuery doInHibernate(Session session) throws HibernateException, SQLException { Criteria newCriteria = session.createCriteria(impl.getEntityOrClassName()); Iterator iterator = impl.iterateExpressionEntries(); while (iterator.hasNext()) { CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) iterator.next(); newCriteria.add(entry.getCriterion()); } Iterator subcriteriaIterator = impl.iterateSubcriteria(); while (subcriteriaIterator.hasNext()) { CriteriaImpl.Subcriteria sub = (CriteriaImpl.Subcriteria) subcriteriaIterator.next(); newCriteria.createAlias(sub.getPath(), sub.getAlias(), sub.getJoinType(), sub.getWithClause()); } return new HibernateQuery(newCriteria, hibernateSession, entity); } }); }
From source file:org.n52.series.db.DataModelUtil.java
License:Open Source License
public static String getSqlString(Criteria criteria) { CriteriaImpl criteriaImpl = (CriteriaImpl) criteria; SessionImplementor session = criteriaImpl.getSession(); SessionFactoryImplementor factory = extractSessionFactory(criteria); CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS); String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName()); CriteriaJoinWalker walker = new CriteriaJoinWalker( (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers()); return walker.getSQLString(); }
From source file:org.n52.sos.ds.hibernate.util.HibernateHelper.java
License:Open Source License
/** * Get the SQL query string from Criteria. * * @param criteria// w w w. ja v a 2 s.co m * Criteria to get SQL query string from * @return SQL query string from criteria */ public static String getSqlString(Criteria criteria) { CriteriaImpl criteriaImpl = (CriteriaImpl) criteria; SessionImplementor session = criteriaImpl.getSession(); SessionFactoryImplementor factory = session.getFactory(); CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS); String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName()); CriteriaJoinWalker walker = new CriteriaJoinWalker( (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers()); return walker.getSQLString(); }
From source file:org.shredzone.cilla.core.repository.impl.SearchDaoHibImpl.java
License:Open Source License
/** * Asserts that the given {@link Criteria} is bound to the correct entity. * * @param crit/*w ww . ja v a2s. c o m*/ * {@link Criteria} to test */ private void assertCriteriaEntity(Criteria crit) { if (crit != null && crit instanceof CriteriaImpl) { CriteriaImpl impl = (CriteriaImpl) crit; String entity = impl.getEntityOrClassName(); if (!(Page.class.getName().equals(entity))) { throw new IllegalArgumentException("Criteria not bound to Page, but " + entity); } } }
From source file:support.SqlUtils.java
public static String getSql(Criteria criteria) { CriteriaImpl criteriaImpl = (CriteriaImpl) criteria; SessionImplementor session = criteriaImpl.getSession(); SessionFactoryImplementor factory = session.getFactory(); CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS); String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName()); CriteriaJoinWalker walker = new CriteriaJoinWalker( (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers()); String sql = walker.getSQLString(); return sql;/*from www .j a v a 2 s . c o m*/ }