Java 类org.hibernate.internal.AbstractQueryImpl 实例源码

项目:lams    文件:NamedQueryLoader.java   
@Override
public Object load(Serializable id, Object optionalObject, SessionImplementor session) {
    LOG.debugf( "Loading entity: %s using named query: %s", persister.getEntityName(), queryName );

    // IMPL NOTE: essentially we perform the named query (which loads the entity into the PC), and then
    // do an internal lookup of the entity from the PC.

    final AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedQuery( queryName );
    if ( query.hasNamedParameters() ) {
        query.setParameter( query.getNamedParameters()[0], id, persister.getIdentifierType() );
    }
    else {
        query.setParameter( 0, id, persister.getIdentifierType() );
    }

    query.setOptionalId( id );
    query.setOptionalEntityName( persister.getEntityName() );
    query.setOptionalObject( optionalObject );
    query.setFlushMode( FlushMode.MANUAL );
    query.list();

    // now look up the object we are really interested in!
    // (this lets us correctly handle proxies and multi-row or multi-column queries)
    return session.getPersistenceContext().getEntity( session.generateEntityKey( id, persister ) );

}
项目:lams    文件:NamedQueryCollectionInitializer.java   
public void initialize(Serializable key, SessionImplementor session)
throws HibernateException {

       LOG.debugf("Initializing collection: %s using named query: %s", persister.getRole(), queryName);

    //TODO: is there a more elegant way than downcasting?
    AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedSQLQuery(queryName);
    if ( query.getNamedParameters().length>0 ) {
        query.setParameter(
                query.getNamedParameters()[0],
                key,
                persister.getKeyType()
            );
    }
    else {
        query.setParameter( 0, key, persister.getKeyType() );
    }
    query.setCollectionKey( key )
            .setFlushMode( FlushMode.MANUAL )
            .list();

}
项目:base-framework    文件:BasicHibernateDao.java   
/**
 * 设置参数值到query的hql中
 *
 * @param query Hibernate Query
 * @param values 参数值可变数组
 */
protected void setQueryValues(Query query ,Object... values) {
    if (ArrayUtils.isEmpty(values)) {
        return ;
    }
    AbstractQueryImpl impl = (AbstractQueryImpl) query;
    String[] params = impl.getNamedParameters();

    int methodParameterPosition = params.length - 1;

    if (impl.hasNamedParameters()) {
        for (String p : params) {
            Object o = values[methodParameterPosition--];
            query.setParameter(p, o);
        }
    } else {
        for (Integer i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
    }
}
项目:OperatieBRP    文件:CustomSimpleQuerycostJpaRepository.java   
private Map<String, TypedValue> getNamedParameterValues(final QueryImpl query) {
    try {
        final Method accessor = AbstractQueryImpl.class.getDeclaredMethod("getNamedParams");
        accessor.setAccessible(true);
        return (Map<String, TypedValue>) accessor.invoke(query);
    } catch (ReflectiveOperationException e) {
        throw new IllegalArgumentException("Could not get named parameter values from query", e);
    }
}
项目:OperatieBRP    文件:CustomSimpleQuerycostJpaRepository.java   
private List<Object> getPositionalParameterValues(final QueryImpl query) {
    try {
        final Method accessor = AbstractQueryImpl.class.getDeclaredMethod("getValues");
        accessor.setAccessible(true);
        return (List<Object>) accessor.invoke(query);
    } catch (ReflectiveOperationException e) {
        throw new IllegalArgumentException("Could not get positional parameter values from query", e);
    }
}
项目:OperatieBRP    文件:CustomSimpleMaxedJpaRepository.java   
@Override
protected TypedQuery<Long> getCountQuery(final Specification<T> spec) {
    final CriteriaBuilder builder = em.getCriteriaBuilder();
    final CriteriaQuery<T> criteria = builder.createQuery(getDomainClass());
    final Root<T> root = applySpecificationToQueryCriteria(spec, criteria);
    criteria.select(root);
    final TypedQuery<T> query = em.createQuery(criteria);


    final AbstractQueryImpl hibernateQuery = query.unwrap(AbstractQueryImpl.class);
    @SuppressWarnings("unchecked")
    final Map<String, TypedValue> pNamedParameters = (Map<String, TypedValue>) getField(hibernateQuery, AbstractQueryImpl.class, "namedParameters");

    final String hql = hibernateQuery.getQueryString();

    final ASTQueryTranslatorFactory queryTranslatorFactory = new ASTQueryTranslatorFactory();
    final SessionImplementor hibernateSession = em.unwrap(SessionImplementor.class);
    final QueryTranslator queryTranslator =
            queryTranslatorFactory.createQueryTranslator("", hql, Collections.emptyMap(), hibernateSession.getFactory(), null);
    queryTranslator.compile(Collections.emptyMap(), false);
    final String sql = queryTranslator.getSQLString();
    final ParameterTranslations paramTranslations = queryTranslator.getParameterTranslations();

    final String countSql = String.format("select count(*) from (%s limit %d) as sqry", sql, maximumRecords + 1);

    final Query nativeQuery = em.createNativeQuery(countSql);


    final AbstractQueryImpl resultQuery = nativeQuery.unwrap(AbstractQueryImpl.class);

    if (pNamedParameters != null) {
        for (final Entry<String, TypedValue> entry : pNamedParameters.entrySet()) {
            for (final int index : paramTranslations.getNamedParameterSqlLocations(entry.getKey())) {

                resultQuery.setParameter(index, entry.getValue().getValue(), entry.getValue().getType());
            }
        }
    }

    return new CustomCountQueryWrapper(nativeQuery);
}
项目:Layer-Query    文件:CustomCriteriaImpl.java   
@Override
public Object uniqueResult() throws HibernateException {
    return AbstractQueryImpl.uniqueElement(list());
}