use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method buildQueryFromName.
protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resultType) {
checkOpen();
try {
checkTransactionSynchStatus();
delayedAfterCompletion();
// todo : apply stored setting at the JPA Query level too
final NamedQueryDefinition namedQueryDefinition = getFactory().getNamedQueryRepository().getNamedQueryDefinition(name);
if (namedQueryDefinition != null) {
return createQuery(namedQueryDefinition, resultType);
}
final NamedSQLQueryDefinition nativeQueryDefinition = getFactory().getNamedQueryRepository().getNamedSQLQueryDefinition(name);
if (nativeQueryDefinition != null) {
return (QueryImplementor<T>) createNativeQuery(nativeQueryDefinition, resultType);
}
throw exceptionConverter.convert(new IllegalArgumentException("No query defined for that name [" + name + "]"));
} catch (RuntimeException e) {
throw !(e instanceof IllegalArgumentException) ? new IllegalArgumentException(e) : e;
}
}
use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.
the class NamedQueryRepository method checkNamedQueries.
public Map<String, HibernateException> checkNamedQueries(QueryPlanCache queryPlanCache) {
Map<String, HibernateException> errors = new HashMap<String, HibernateException>();
// Check named HQL queries
log.debugf("Checking %s named HQL queries", namedQueryDefinitionMap.size());
for (NamedQueryDefinition namedQueryDefinition : namedQueryDefinitionMap.values()) {
// this will throw an error if there's something wrong.
try {
log.debugf("Checking named query: %s", namedQueryDefinition.getName());
// TODO: BUG! this currently fails for named queries for non-POJO entities
queryPlanCache.getHQLQueryPlan(namedQueryDefinition.getQueryString(), false, Collections.EMPTY_MAP);
} catch (HibernateException e) {
errors.put(namedQueryDefinition.getName(), e);
}
}
// Check native-sql queries
log.debugf("Checking %s named SQL queries", namedSqlQueryDefinitionMap.size());
for (NamedSQLQueryDefinition namedSQLQueryDefinition : namedSqlQueryDefinitionMap.values()) {
// this will throw an error if there's something wrong.
try {
log.debugf("Checking named SQL query: %s", namedSQLQueryDefinition.getName());
// TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
// currently not doable though because of the resultset-ref stuff...
NativeSQLQuerySpecification spec;
if (namedSQLQueryDefinition.getResultSetRef() != null) {
ResultSetMappingDefinition definition = getResultSetMappingDefinition(namedSQLQueryDefinition.getResultSetRef());
if (definition == null) {
throw new MappingException("Unable to find resultset-ref definition: " + namedSQLQueryDefinition.getResultSetRef());
}
spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), definition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
} else {
spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), namedSQLQueryDefinition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
}
queryPlanCache.getNativeSQLQueryPlan(spec);
} catch (HibernateException e) {
errors.put(namedSQLQueryDefinition.getName(), e);
}
}
return errors;
}
use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.
the class NamedQueryRepository method registerNamedQueryDefinition.
public synchronized void registerNamedQueryDefinition(String name, NamedQueryDefinition definition) {
if (NamedSQLQueryDefinition.class.isInstance(definition)) {
throw new IllegalArgumentException("NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition");
}
if (!name.equals(definition.getName())) {
definition = definition.makeCopy(name);
}
final Map<String, NamedQueryDefinition> copy = CollectionHelper.makeCopy(namedQueryDefinitionMap);
final NamedQueryDefinition previous = copy.put(name, definition);
if (previous != null) {
log.debugf("registering named query definition [%s] overriding previously registered definition [%s]", name, previous);
}
this.namedQueryDefinitionMap = Collections.unmodifiableMap(copy);
}
use of org.hibernate.engine.spi.NamedQueryDefinition 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);
}
use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.
the class QueryBinder method bindQuery.
public static void bindQuery(NamedQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
if (queryAnn == null)
return;
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
// EJBQL Query
QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
String queryName = queryAnn.query();
NamedQueryDefinition queryDefinition = new NamedQueryDefinitionBuilder(queryAnn.name()).setLockOptions(hints.determineLockOptions(queryAnn)).setQuery(queryName).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).createNamedQueryDefinition();
if (isDefault) {
context.getMetadataCollector().addDefaultQuery(queryDefinition);
} else {
context.getMetadataCollector().addNamedQuery(queryDefinition);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named query: %s => %s", queryDefinition.getName(), queryDefinition.getQueryString());
}
}
Aggregations