Search in sources :

Example 1 with NamedQueryMemento

use of org.hibernate.query.named.NamedQueryMemento in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method postInstantiate.

// private class ColumnMapperImpl implements ColumnMapper {
// @Override
// public SqlValueReference[] map(String reference) {
// final String[] columnNames;
// final String[] formulaTemplates;
// 
// // handle the special "$element$" property name...
// if ( "$element$".equals( reference ) ) {
// columnNames = elementColumnNames;
// formulaTemplates = elementFormulaTemplates;
// }
// else {
// columnNames = elementPropertyMapping.toColumns( reference );
// formulaTemplates = formulaTemplates( reference, columnNames.length );
// }
// 
// final SqlValueReference[] result = new SqlValueReference[ columnNames.length ];
// int i = 0;
// for ( final String columnName : columnNames ) {
// if ( columnName == null ) {
// // if the column name is null, it indicates that this index in the property value mapping is
// // actually represented by a formula.
// //					final int propertyIndex = elementPersister.getEntityMetamodel().getPropertyIndex( reference );
// final String formulaTemplate = formulaTemplates[i];
// result[i] = new FormulaReference() {
// @Override
// public String getFormulaFragment() {
// return formulaTemplate;
// }
// };
// }
// else {
// result[i] = new ColumnReference() {
// @Override
// public String getColumnName() {
// return columnName;
// }
// };
// }
// i++;
// }
// return result;
// }
// }
// private String[] formulaTemplates(String reference, int expectedSize) {
// try {
// final int propertyIndex = elementPersister.getEntityMetamodel().getPropertyIndex( reference );
// return  ( (Queryable) elementPersister ).getSubclassPropertyFormulaTemplateClosure()[propertyIndex];
// }
// catch (Exception e) {
// return new String[expectedSize];
// }
// }
@Override
public void postInstantiate() throws MappingException {
    if (queryLoaderName == null) {
        collectionLoader = createCollectionLoader(LoadQueryInfluencers.NONE);
    } else {
        // We pass null as metamodel because we did the initialization during construction already
        final NamedQueryMemento namedQueryMemento = factory.getQueryEngine().getNamedObjectRepository().resolve(factory, null, queryLoaderName);
        collectionLoader = new CollectionLoaderNamedQuery(this, namedQueryMemento);
    }
    if (attributeMapping.getIndexDescriptor() != null) {
        collectionElementLoaderByIndex = new CollectionElementLoaderByIndex(attributeMapping, baseIndex, LoadQueryInfluencers.NONE, getFactory());
    }
}
Also used : CollectionLoaderNamedQuery(org.hibernate.loader.ast.internal.CollectionLoaderNamedQuery) CollectionElementLoaderByIndex(org.hibernate.loader.ast.internal.CollectionElementLoaderByIndex) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento)

Example 2 with NamedQueryMemento

use of org.hibernate.query.named.NamedQueryMemento in project hibernate-orm by hibernate.

the class NamedObjectRepositoryImpl method resolve.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare repository for use
@Override
public NamedQueryMemento resolve(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, String registrationName) {
    NamedQueryMemento namedQuery = sqlMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    namedQuery = sqmMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    namedQuery = callableMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    final NamedHqlQueryDefinition namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping(registrationName);
    if (namedHqlQueryDefinition != null) {
        final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
        sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    final NamedNativeQueryDefinition namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping(registrationName);
    if (namedNativeQueryDefinition != null) {
        final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
        sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    final NamedProcedureCallDefinition namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping(registrationName);
    if (namedCallableQueryDefinition != null) {
        final NamedCallableQueryMemento resolved = namedCallableQueryDefinition.resolve(sessionFactory);
        callableMementoMap.put(namedCallableQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    return null;
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedHqlQueryDefinition(org.hibernate.boot.query.NamedHqlQueryDefinition) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento) NamedNativeQueryDefinition(org.hibernate.boot.query.NamedNativeQueryDefinition) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition)

Example 3 with NamedQueryMemento

use of org.hibernate.query.named.NamedQueryMemento in project hibernate-orm by hibernate.

the class SessionFactoryImpl method addNamedQuery.

@Override
public void addNamedQuery(String name, Query query) {
    validateNotClosed();
    // NOTE : we use Query#unwrap here (rather than direct type checking) to account for possibly wrapped
    // query implementations
    // first, handle StoredProcedureQuery
    final NamedObjectRepository namedObjectRepository = getQueryEngine().getNamedObjectRepository();
    try {
        final ProcedureCallImplementor<?> unwrapped = query.unwrap(ProcedureCallImplementor.class);
        if (unwrapped != null) {
            namedObjectRepository.registerCallableQueryMemento(name, unwrapped.toMemento(name));
            return;
        }
    } catch (PersistenceException ignore) {
    // this means 'query' is not a ProcedureCallImplementor
    }
    // then try as a native-SQL or JPQL query
    try {
        QueryImplementor<?> hibernateQuery = query.unwrap(QueryImplementor.class);
        if (hibernateQuery != null) {
            // create and register the proper NamedQueryDefinition...
            if (hibernateQuery instanceof NativeQueryImplementor) {
                namedObjectRepository.registerNativeQueryMemento(name, ((NativeQueryImplementor<?>) hibernateQuery).toMemento(name));
            } else {
                final NamedQueryMemento namedQueryMemento = ((SqmQueryImplementor<?>) hibernateQuery).toMemento(name);
                namedObjectRepository.registerSqmQueryMemento(name, (NamedSqmQueryMemento) namedQueryMemento);
            }
            return;
        }
    } catch (PersistenceException ignore) {
    // this means 'query' is not a native-SQL or JPQL query
    }
    // if we get here, we are unsure how to properly unwrap the incoming query to extract the needed information
    throw new PersistenceException(String.format("Unsure how to properly unwrap given Query [%s] as basis for named query", query));
}
Also used : NativeQueryImplementor(org.hibernate.query.sql.spi.NativeQueryImplementor) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) PersistenceException(jakarta.persistence.PersistenceException) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento)

Aggregations

NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)3 PersistenceException (jakarta.persistence.PersistenceException)1 NamedHqlQueryDefinition (org.hibernate.boot.query.NamedHqlQueryDefinition)1 NamedNativeQueryDefinition (org.hibernate.boot.query.NamedNativeQueryDefinition)1 NamedProcedureCallDefinition (org.hibernate.boot.query.NamedProcedureCallDefinition)1 CollectionElementLoaderByIndex (org.hibernate.loader.ast.internal.CollectionElementLoaderByIndex)1 CollectionLoaderNamedQuery (org.hibernate.loader.ast.internal.CollectionLoaderNamedQuery)1 NamedCallableQueryMemento (org.hibernate.procedure.spi.NamedCallableQueryMemento)1 SqmQueryImplementor (org.hibernate.query.hql.spi.SqmQueryImplementor)1 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)1 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)1 NativeQueryImplementor (org.hibernate.query.sql.spi.NativeQueryImplementor)1 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)1