use of org.hibernate.engine.spi.NamedSQLQueryDefinition in project hibernate-orm by hibernate.
the class NamedQueryBinder method processNamedNativeQuery.
public static void processNamedNativeQuery(final HbmLocalMetadataBuildingContext context, JaxbHbmNamedNativeQueryType namedQueryBinding, String prefix) {
final String queryName = prefix + namedQueryBinding.getName();
final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder().setName(queryName).setComment(namedQueryBinding.getComment()).setCacheable(namedQueryBinding.isCacheable()).setCacheMode(namedQueryBinding.getCacheMode()).setCacheRegion(namedQueryBinding.getCacheRegion()).setTimeout(namedQueryBinding.getTimeout()).setReadOnly(namedQueryBinding.isReadOnly()).setFlushMode(namedQueryBinding.getFlushMode()).setFetchSize(namedQueryBinding.getFetchSize()).setCallable(namedQueryBinding.isCallable()).setResultSetRef(namedQueryBinding.getResultsetRef());
final ImplicitResultSetMappingDefinition.Builder implicitResultSetMappingBuilder = new ImplicitResultSetMappingDefinition.Builder(queryName);
boolean foundQuery = false;
for (Object content : namedQueryBinding.getContent()) {
final boolean wasQuery = processNamedQueryContentItem(content, builder, implicitResultSetMappingBuilder, namedQueryBinding, context);
if (wasQuery) {
foundQuery = true;
}
}
if (!foundQuery) {
throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] did not specify query string", namedQueryBinding.getName()), context.getOrigin());
}
if (implicitResultSetMappingBuilder.hasAnyReturns()) {
if (StringHelper.isNotEmpty(namedQueryBinding.getResultsetRef())) {
throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] specified both a resultset-ref and an inline mapping of results", namedQueryBinding.getName()), context.getOrigin());
}
// Building a ResultSet mapping needs access to entity bindings for any entity
// returns it defines. But binding for those entities may have not been
// completed yet. For "normal" ResultSet mappings, this is already handled by
// the fact that MetadataSourceProcessor#processResultSetMappings() is called
// afterQuery all entity hierarchies have been processed. However, here we are in
// the middle of processing named-queries (either top-level or entity-level)
// and have no guarantee that any entity bindings we may need here are bound.
// So we add the second-pass to bind the implicit resultSet mapping.
//
// It is possible to know here whether the second-pass is needed or whether we
// can immediately bind the ResultSet mapping.
// todo : consider implementing this (^^) checking
final ImplicitResultSetMappingDefinition implicitResultSetMappingDefinition = implicitResultSetMappingBuilder.build();
builder.setResultSetRef(implicitResultSetMappingDefinition.getName());
context.getMetadataCollector().addSecondPass(new SecondPass() {
@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
final ResultSetMappingDefinition resultSetMappingDefinition = ResultSetMappingBinder.bind(implicitResultSetMappingDefinition, context);
context.getMetadataCollector().addResultSetMapping(resultSetMappingDefinition);
NativeSQLQueryReturn[] newQueryReturns = resultSetMappingDefinition.getQueryReturns();
final NamedSQLQueryDefinition queryDefinition = context.getMetadataCollector().getNamedNativeQueryDefinition(queryName);
if (queryDefinition != null) {
queryDefinition.addQueryReturns(newQueryReturns);
}
}
});
}
context.getMetadataCollector().addNamedNativeQuery(builder.createNamedQueryDefinition());
}
use of org.hibernate.engine.spi.NamedSQLQueryDefinition in project hibernate-orm by hibernate.
the class QueryBinder method bindNativeQuery.
public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
if (queryAnn == null) {
return;
}
//ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
NamedSQLQueryDefinition query;
String resultSetMapping = queryAnn.resultSetMapping();
if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
//sql result set usage
query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setResultSetRef(resultSetMapping).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
} else if (!void.class.equals(queryAnn.resultClass())) {
//class mapping usage
//FIXME should be done in a second pass due to entity name?
final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn }).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
} else {
throw new NotYetImplementedException("Pure native scalar queries are not yet supported");
}
context.getMetadataCollector().addNamedNativeQuery(query);
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named native query: %s => %s", query.getName(), queryAnn.query());
}
}
use of org.hibernate.engine.spi.NamedSQLQueryDefinition in project hibernate-orm by hibernate.
the class QueryBinder method bindNativeQuery.
public static void bindNativeQuery(NamedNativeQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
if (queryAnn == null)
return;
//ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
String resultSetMapping = queryAnn.resultSetMapping();
QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
String queryName = queryAnn.query();
NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(queryAnn.name()).setQuery(queryName).setQuerySpaces(null).setCacheable(hints.getBoolean(queryName, QueryHints.CACHEABLE)).setCacheRegion(hints.getString(queryName, QueryHints.CACHE_REGION)).setTimeout(hints.getTimeout(queryName)).setFetchSize(hints.getInteger(queryName, QueryHints.FETCH_SIZE)).setFlushMode(hints.getFlushMode(queryName)).setCacheMode(hints.getCacheMode(queryName)).setReadOnly(hints.getBoolean(queryName, QueryHints.READ_ONLY)).setComment(hints.getString(queryName, QueryHints.COMMENT)).setParameterTypes(null).setCallable(hints.getBoolean(queryName, QueryHints.CALLABLE));
if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
//sql result set usage
builder.setResultSetRef(resultSetMapping).createNamedQueryDefinition();
} else if (!void.class.equals(queryAnn.resultClass())) {
//class mapping usage
//FIXME should be done in a second pass due to entity name?
final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
builder.setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn });
} else {
builder.setQueryReturns(new NativeSQLQueryReturn[0]);
}
NamedSQLQueryDefinition query = builder.createNamedQueryDefinition();
if (isDefault) {
context.getMetadataCollector().addDefaultNamedNativeQuery(query);
} else {
context.getMetadataCollector().addNamedNativeQuery(query);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named native query: %s => %s", queryAnn.name(), queryAnn.query());
}
}
use of org.hibernate.engine.spi.NamedSQLQueryDefinition in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method getNamedNativeQuery.
@Override
public NativeQueryImplementor getNamedNativeQuery(String name) {
checkOpen();
checkTransactionSynchStatus();
delayedAfterCompletion();
final NamedSQLQueryDefinition nativeQueryDefinition = factory.getNamedQueryRepository().getNamedSQLQueryDefinition(name);
if (nativeQueryDefinition != null) {
return createNativeQuery(nativeQueryDefinition, true);
}
throw exceptionConverter.convert(new IllegalArgumentException("No query defined for that name [" + name + "]"));
}
use of org.hibernate.engine.spi.NamedSQLQueryDefinition in project hibernate-orm by hibernate.
the class NamedQueryRepository method registerNamedSQLQueryDefinition.
public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) {
if (!name.equals(definition.getName())) {
definition = definition.makeCopy(name);
}
final Map<String, NamedSQLQueryDefinition> copy = CollectionHelper.makeCopy(namedSqlQueryDefinitionMap);
final NamedQueryDefinition previous = copy.put(name, definition);
if (previous != null) {
log.debugf("registering named SQL query definition [%s] overriding previously registered definition [%s]", name, previous);
}
this.namedSqlQueryDefinitionMap = Collections.unmodifiableMap(copy);
}
Aggregations