Java 类org.hibernate.hql.spi.ParameterTranslations 实例源码

项目:lams    文件:QueryTranslatorImpl.java   
@Override
public ParameterTranslations getParameterTranslations() {
    if ( paramTranslations == null ) {
        paramTranslations = new ParameterTranslationsImpl( getWalker().getParameters() );
    }
    return paramTranslations;
}
项目:strqlbuilder    文件:StrQLBuilder.java   
private Map<Integer, Object> getPositionParametersMap(QueryTranslator translator, Map<String, Object> parametersMap)
{
    Map<Integer, Object> positionParametersMap = new HashMap<Integer, Object>();
    ParameterTranslations parameterTranslations = translator.getParameterTranslations();
    for (Entry<String, Object> parameter : parametersMap.entrySet())
    {
        String name = parameter.getKey();
        for (int position : parameterTranslations.getNamedParameterSqlLocations(name))
        {
            positionParametersMap.put(position + 1, parameter.getValue());
            // Note that the +1 on the position is needed because of a mismatch between 0-based and 1-based indexing of both APIs.
        }
    }
    return positionParametersMap;
}
项目:lams    文件:HQLQueryPlan.java   
private ParameterMetadata buildParameterMetadata(ParameterTranslations parameterTranslations, String hql) {
    final long start = TRACE_ENABLED ? System.nanoTime() : 0;
    final ParamLocationRecognizer recognizer = ParamLocationRecognizer.parseLocations( hql );

    if ( TRACE_ENABLED ) {
        final long end = System.nanoTime();
        LOG.tracev( "HQL param location recognition took {0} nanoseconds ({1})", ( end - start ), hql );
    }

    int ordinalParamCount = parameterTranslations.getOrdinalParameterCount();
    final int[] locations = ArrayHelper.toIntArray( recognizer.getOrdinalParameterLocationList() );
    if ( parameterTranslations.supportsOrdinalParameterMetadata() && locations.length != ordinalParamCount ) {
        throw new HibernateException( "ordinal parameter mismatch" );
    }
    ordinalParamCount = locations.length;

    final OrdinalParameterDescriptor[] ordinalParamDescriptors = new OrdinalParameterDescriptor[ordinalParamCount];
    for ( int i = 1; i <= ordinalParamCount; i++ ) {
        ordinalParamDescriptors[ i - 1 ] = new OrdinalParameterDescriptor(
                i,
                parameterTranslations.supportsOrdinalParameterMetadata()
                        ? parameterTranslations.getOrdinalParameterExpectedType( i )
                        : null,
                locations[ i - 1 ]
        );
    }

    final Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>();
    final Map<String, ParamLocationRecognizer.NamedParameterDescription> map = recognizer.getNamedParameterDescriptionMap();
    for ( final String name : map.keySet() ) {
        final ParamLocationRecognizer.NamedParameterDescription description = map.get( name );
        namedParamDescriptorMap.put(
                name,
                new NamedParameterDescriptor(
                        name,
                        parameterTranslations.getNamedParameterExpectedType( name ),
                        description.buildPositionsArray(),
                        description.isJpaStyle()
                )
        );
    }
    return new ParameterMetadata( ordinalParamDescriptors, namedParamDescriptorMap );
}
项目: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);
}
项目:starter-kit-spring-maven    文件:StandardJpaDao.java   
private Long getTotal(TypedQuery<T> query) {
    String hqlQuery = query.unwrap(org.hibernate.Query.class).getQueryString();

    QueryTranslator translator = translatorFactory.createQueryTranslator(hqlQuery, hqlQuery, EMPTY, sessionFactory, null);

    translator.compile(EMPTY, false);

    String sqlQuery = new StringBuilder("SELECT COUNT(*) FROM (").append(translator.getSQLString())
            .append(") x").toString();

    Query countQuery = entityManager.createNativeQuery(sqlQuery);

    ParameterTranslations parameterTranslations = translator.getParameterTranslations();

    query.getParameters().stream().forEach( parameter -> {
        String name = parameter.getName();

        for(int position : parameterTranslations.getNamedParameterSqlLocations(name)) {
            countQuery.setParameter(position+1, query.getParameterValue(name));
        };
    });

    return ((Number) countQuery.getSingleResult()).longValue();
}