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());
}
}
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;
}
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));
}
Aggregations