Java Code Examples for org.hibernate.engine.spi.NamedSQLQueryDefinition

The following examples show how to use org.hibernate.engine.spi.NamedSQLQueryDefinition. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private <T> ReactiveQuery<T> buildReactiveQueryFromName(String name, Class<T> resultType) {
	checkOpen();
	try {
		pulseTransactionCoordinator();
		delayedAfterCompletion();

		// todo : apply stored setting at the JPA Query level too

		final NamedQueryDefinition namedQueryDefinition = getFactory().getNamedQueryRepository().getNamedQueryDefinition( name );
		if ( namedQueryDefinition != null ) {
			return createReactiveQuery( namedQueryDefinition, resultType );
		}

		final NamedSQLQueryDefinition nativeQueryDefinition = getFactory().getNamedQueryRepository().getNamedSQLQueryDefinition( name );
		if ( nativeQueryDefinition != null ) {
			return createReactiveNativeQuery( nativeQueryDefinition, resultType );
		}

		throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
	}
	catch (RuntimeException e) {
		throw !( e instanceof IllegalArgumentException ) ? new IllegalArgumentException( e ) : e;
	}
}
 
Example 2
private <T> ReactiveNativeQuery<T> createReactiveNativeQuery(NamedSQLQueryDefinition queryDefinition, Class<T> resultType) {
	if ( resultType != null && !Tuple.class.equals( resultType ) && !Object[].class.equals( resultType ) ) {
		resultClassChecking( resultType, queryDefinition );
	}

	final ReactiveNativeQueryImpl<T> query = new ReactiveNativeQueryImpl<>(
			queryDefinition,
			this,
			getFactory().getQueryPlanCache().getSQLParameterMetadata( queryDefinition.getQueryString(), false )
	);
	if ( Tuple.class.equals( resultType ) ) {
		query.setResultTransformer( new NativeQueryTupleTransformer() );
	}
	applyQuerySettingsAndHints( query );
	query.setHibernateFlushMode( queryDefinition.getFlushMode() );
	query.setComment( queryDefinition.getComment() != null ? queryDefinition.getComment() : queryDefinition.getName() );
	if ( queryDefinition.getLockOptions() != null ) {
		query.setLockOptions( queryDefinition.getLockOptions() );
	}

	initQueryFromNamedDefinition( query, queryDefinition );

	return query;
}
 
Example 3
Source Project: lams   Source File: NamedQueryRepository.java    License: GNU General Public License v2.0 6 votes vote down vote up
public NamedQueryRepository(
		Iterable<NamedQueryDefinition> namedQueryDefinitions,
		Iterable<NamedSQLQueryDefinition> namedSqlQueryDefinitions,
		Iterable<ResultSetMappingDefinition> namedSqlResultSetMappings,
		Map<String, ProcedureCallMemento> namedProcedureCalls) {
	final HashMap<String, NamedQueryDefinition> namedQueryDefinitionMap = new HashMap<String, NamedQueryDefinition>();
	for ( NamedQueryDefinition namedQueryDefinition : namedQueryDefinitions ) {
		namedQueryDefinitionMap.put( namedQueryDefinition.getName(), namedQueryDefinition );
	}
	this.namedQueryDefinitionMap = Collections.unmodifiableMap( namedQueryDefinitionMap );


	final HashMap<String, NamedSQLQueryDefinition> namedSqlQueryDefinitionMap = new HashMap<String, NamedSQLQueryDefinition>();
	for ( NamedSQLQueryDefinition namedSqlQueryDefinition : namedSqlQueryDefinitions ) {
		namedSqlQueryDefinitionMap.put( namedSqlQueryDefinition.getName(), namedSqlQueryDefinition );
	}
	this.namedSqlQueryDefinitionMap = Collections.unmodifiableMap( namedSqlQueryDefinitionMap );

	final HashMap<String, ResultSetMappingDefinition> namedSqlResultSetMappingMap = new HashMap<String, ResultSetMappingDefinition>();
	for ( ResultSetMappingDefinition resultSetMappingDefinition : namedSqlResultSetMappings ) {
		namedSqlResultSetMappingMap.put( resultSetMappingDefinition.getName(), resultSetMappingDefinition );
	}
	this.namedSqlResultSetMappingMap = Collections.unmodifiableMap( namedSqlResultSetMappingMap );
	this.procedureCallMementoMap = Collections.unmodifiableMap( namedProcedureCalls );
}
 
Example 4
Source Project: lams   Source File: NamedQueryRepository.java    License: GNU General Public License v2.0 6 votes vote down vote up
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 );
}
 
Example 5
Source Project: lams   Source File: NamedQueryRepository.java    License: GNU General Public License v2.0 6 votes vote down vote up
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 );
}
 
Example 6
@Override
public QueryImplementor getNamedQuery(String name) {
	checkOpen();
	checkTransactionSynchStatus();
	delayedAfterCompletion();

	// look as HQL/JPQL first
	final NamedQueryDefinition queryDefinition = factory.getNamedQueryRepository().getNamedQueryDefinition( name );
	if ( queryDefinition != null ) {
		return createQuery( queryDefinition );
	}

	// then as a native query
	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 + "]" ) );
}
 
Example 7
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;
	}
}
 
Example 8
@SuppressWarnings({"WeakerAccess", "unchecked"})
protected <T> NativeQueryImplementor createNativeQuery(NamedSQLQueryDefinition queryDefinition, Class<T> resultType) {
	if ( resultType != null && !Tuple.class.equals(resultType)) {
		resultClassChecking( resultType, queryDefinition );
	}

	final NativeQueryImpl query = new NativeQueryImpl(
			queryDefinition,
			this,
			factory.getQueryPlanCache().getSQLParameterMetadata( queryDefinition.getQueryString(), false )
	);
	if (Tuple.class.equals(resultType)) {
		query.setResultTransformer(new NativeQueryTupleTransformer());
	}
	query.setHibernateFlushMode( queryDefinition.getFlushMode() );
	query.setComment( queryDefinition.getComment() != null ? queryDefinition.getComment() : queryDefinition.getName() );
	if ( queryDefinition.getLockOptions() != null ) {
		query.setLockOptions( queryDefinition.getLockOptions() );
	}

	initQueryFromNamedDefinition( query, queryDefinition );
	applyQuerySettingsAndHints( query );

	return query;
}
 
Example 9
Source Project: base-framework   Source File: BasicHibernateDao.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 根据查询SQL与参数列表创建SQLQuery对象
 * 
 * @param queryOrNamedSQLQuery query 或者 NamedSQLQuery
 * @param values 数量可变的参数,按顺序绑定.
 *            
 * @return {@link SQLQuery}
 */
protected SQLQuery createSQLQuery( String queryOrNamedSQLQuery,  Object... values) {
	Assert.hasText(queryOrNamedSQLQuery, "queryOrNamedSQLQuery不能为空");
	SessionFactoryImpl factory = (SessionFactoryImpl) sessionFactory;
	NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery( queryOrNamedSQLQuery );
	Query query = null;
	
	if (nsqlqd != null) {
		query = getSession().getNamedQuery(queryOrNamedSQLQuery);
	} else {
		query = getSession().createSQLQuery(queryOrNamedSQLQuery);
	}
	
	setQueryValues(query, values);
	SQLQuery sqlQuery = (SQLQuery)query;
	
	return sqlQuery.addEntity(entityClass);
}
 
Example 10
Source Project: lams   Source File: Configuration.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected void reset() {
	implicitNamingStrategy = ImplicitNamingStrategyJpaCompliantImpl.INSTANCE;
	physicalNamingStrategy = PhysicalNamingStrategyStandardImpl.INSTANCE;
	namedQueries = new HashMap<String,NamedQueryDefinition>();
	namedSqlQueries = new HashMap<String,NamedSQLQueryDefinition>();
	sqlResultSetMappings = new HashMap<String, ResultSetMappingDefinition>();
	namedEntityGraphMap = new HashMap<String, NamedEntityGraphDefinition>();
	namedProcedureCallMap = new HashMap<String, NamedProcedureCallDefinition>(  );

	standardServiceRegistryBuilder = new StandardServiceRegistryBuilder( bootstrapServiceRegistry );
	entityTuplizerFactory = new EntityTuplizerFactory();
	interceptor = EmptyInterceptor.INSTANCE;
	properties = new Properties(  );
	properties.putAll( standardServiceRegistryBuilder.getSettings());
}
 
Example 11
Source Project: lams   Source File: NamedQueryRepository.java    License: GNU General Public License v2.0 5 votes vote down vote up
public NamedQueryRepository(
		Map<String,NamedQueryDefinition> namedQueryDefinitionMap,
		Map<String,NamedSQLQueryDefinition> namedSqlQueryDefinitionMap,
		Map<String,ResultSetMappingDefinition> namedSqlResultSetMappingMap,
		Map<String, ProcedureCallMemento> namedProcedureCallMap) {
	this.namedQueryDefinitionMap = Collections.unmodifiableMap( namedQueryDefinitionMap );
	this.namedSqlQueryDefinitionMap = Collections.unmodifiableMap( namedSqlQueryDefinitionMap );
	this.namedSqlResultSetMappingMap = Collections.unmodifiableMap( namedSqlResultSetMappingMap );
	this.procedureCallMementoMap = Collections.unmodifiableMap( namedProcedureCallMap );
}
 
Example 12
Source Project: lams   Source File: NativeQueryImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a NativeQueryImpl given a sql query defined in the mappings.
 *
 * @param queryDef The representation of the defined <sql-query/>.
 * @param session The session to which this NativeQuery belongs.
 * @param parameterMetadata Metadata about parameters found in the query.
 */
public NativeQueryImpl(
		NamedSQLQueryDefinition queryDef,
		SharedSessionContractImplementor session,
		ParameterMetadata parameterMetadata) {
	super( session, parameterMetadata );

	this.sqlString = queryDef.getQueryString();
	this.callable = queryDef.isCallable();
	this.querySpaces = queryDef.getQuerySpaces() == null ? null : new ArrayList<>( queryDef.getQuerySpaces() );

	if ( queryDef.getResultSetRef() != null ) {
		ResultSetMappingDefinition definition = session.getFactory()
				.getNamedQueryRepository()
				.getResultSetMappingDefinition( queryDef.getResultSetRef() );
		if ( definition == null ) {
			throw new MappingException(
					"Unable to find resultset-ref definition: " +
							queryDef.getResultSetRef()
			);
		}
		this.queryReturns = new ArrayList<>( Arrays.asList( definition.getQueryReturns() ) );
	}
	else if ( queryDef.getQueryReturns() != null && queryDef.getQueryReturns().length > 0 ) {
		this.queryReturns = new ArrayList<>( Arrays.asList( queryDef.getQueryReturns() ) );
	}
	else {
		this.queryReturns = new ArrayList<>();
	}


	this.queryParameterBindings = QueryParameterBindingsImpl.from(
			parameterMetadata,
			session.getFactory(),
			session.isQueryParametersValidationEnabled()
	);
}
 
Example 13
Source Project: lams   Source File: SessionFactoryImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
private NamedSQLQueryDefinition extractSqlQueryDefinition(org.hibernate.query.NativeQuery nativeSqlQuery, String name) {
	final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder( name );
	fillInNamedQueryBuilder( builder, nativeSqlQuery );
	builder.setCallable( nativeSqlQuery.isCallable() )
			.setQuerySpaces( nativeSqlQuery.getSynchronizedQuerySpaces() )
			.setQueryReturns( nativeSqlQuery.getQueryReturns() );
	return builder.createNamedQueryDefinition();
}
 
Example 14
private NativeQueryImplementor createNativeQuery(NamedSQLQueryDefinition queryDefinition, boolean isOrdinalParameterZeroBased) {
	final ParameterMetadata parameterMetadata = factory.getQueryPlanCache().getSQLParameterMetadata(
			queryDefinition.getQueryString(),
			isOrdinalParameterZeroBased
	);
	return getNativeQueryImplementor( queryDefinition, parameterMetadata );
}
 
Example 15
private NativeQueryImplementor getNativeQueryImplementor(
		NamedSQLQueryDefinition queryDefinition,
		ParameterMetadata parameterMetadata) {
	final NativeQueryImpl query = new NativeQueryImpl(
			queryDefinition,
			this,
			parameterMetadata
	);
	query.setComment( queryDefinition.getComment() != null ? queryDefinition.getComment() : queryDefinition.getName() );

	initQueryFromNamedDefinition( query, queryDefinition );
	applyQuerySettingsAndHints( query );

	return query;
}
 
Example 16
@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 + "]" ) );
}
 
Example 17
@Override
public void addNamedNativeQuery(NamedSQLQueryDefinition def) {
	if ( def == null ) {
		throw new IllegalArgumentException( "Named native query definition object is null" );
	}
	if ( def.getName() == null ) {
		throw new IllegalArgumentException( "Named native query definition name is null: " + def.getQueryString() );
	}

	if ( defaultNamedNativeQueryNames.contains( def.getName() ) ) {
		return;
	}

	applyNamedNativeQuery( def.getName(), def );
}
 
Example 18
public ReactiveNativeQueryImpl(
		NamedSQLQueryDefinition queryDef,
		SharedSessionContractImplementor session,
		ParameterMetadata parameterMetadata) {
	super(queryDef, session, parameterMetadata);
}
 
Example 19
Source Project: quarkus   Source File: PrevalidatedQuarkusMetadata.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(final String name) {
    return metadata.getNamedNativeQueryDefinition(name);
}
 
Example 20
Source Project: quarkus   Source File: PrevalidatedQuarkusMetadata.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
    return metadata.getNamedNativeQueryDefinitions();
}
 
Example 21
Source Project: lams   Source File: QueryBinder.java    License: GNU General Public License v2.0 4 votes vote down vote up
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() );
	}
}
 
Example 22
Source Project: lams   Source File: NamedQueryRepository.java    License: GNU General Public License v2.0 4 votes vote down vote up
public NamedSQLQueryDefinition getNamedSQLQueryDefinition(String queryName) {
	return namedSqlQueryDefinitionMap.get( queryName );
}
 
Example 23
@SuppressWarnings({"unchecked", "WeakerAccess"})
protected void resultClassChecking(Class resultType, NamedSQLQueryDefinition namedQueryDefinition) {
	final NativeSQLQueryReturn[] queryReturns;
	if ( namedQueryDefinition.getQueryReturns() != null ) {
		queryReturns = namedQueryDefinition.getQueryReturns();
	}
	else if ( namedQueryDefinition.getResultSetRef() != null ) {
		final ResultSetMappingDefinition rsMapping = getFactory().getNamedQueryRepository().getResultSetMappingDefinition( namedQueryDefinition.getResultSetRef() );
		queryReturns = rsMapping.getQueryReturns();
	}
	else {
		throw new AssertionFailure( "Unsupported named query model. Please report the bug in Hibernate EntityManager");
	}

	if ( queryReturns.length > 1 ) {
		throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
	}

	final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];

	if ( nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn ) {
		final Class<?> actualReturnedClass;
		final String entityClassName = ( (NativeSQLQueryRootReturn) nativeSQLQueryReturn ).getReturnEntityName();
		try {
			actualReturnedClass = getFactory().getServiceRegistry().getService( ClassLoaderService.class ).classForName( entityClassName );
		}
		catch ( ClassLoadingException e ) {
			throw new AssertionFailure(
					"Unable to load class [" + entityClassName + "] declared on named native query [" +
							namedQueryDefinition.getName() + "]"
			);
		}
		if ( !resultType.isAssignableFrom( actualReturnedClass ) ) {
			throw buildIncompatibleException( resultType, actualReturnedClass );
		}
	}
	else if ( nativeSQLQueryReturn instanceof NativeSQLQueryConstructorReturn ) {
		final NativeSQLQueryConstructorReturn ctorRtn = (NativeSQLQueryConstructorReturn) nativeSQLQueryReturn;
		if ( !resultType.isAssignableFrom( ctorRtn.getTargetClass() ) ) {
			throw buildIncompatibleException( resultType, ctorRtn.getTargetClass() );
		}
	}
	else {
		log.debugf( "Skiping unhandled NativeSQLQueryReturn type : " + nativeSQLQueryReturn );
	}
}
 
Example 24
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(String name) {
	return delegate.getNamedNativeQueryDefinition( name );
}
 
Example 25
@Override
public Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
	return delegate.getNamedNativeQueryDefinitions();
}
 
Example 26
Source Project: lams   Source File: NamedQueryBinder.java    License: GNU General Public License v2.0 4 votes vote down vote up
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
		// after 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() );
}
 
Example 27
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(String name) {
	return namedNativeQueryMap.get( name );
}
 
Example 28
@Override
public java.util.Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
	return namedNativeQueryMap.values();
}
 
Example 29
private void applyNamedNativeQuery(String name, NamedSQLQueryDefinition query) {
	checkQueryName( name );
	namedNativeQueryMap.put( name.intern(), query );
}
 
Example 30
@Override
public void addDefaultNamedNativeQuery(NamedSQLQueryDefinition query) {
	applyNamedNativeQuery( query.getName(), query );
	defaultNamedNativeQueryNames.add( query.getName() );
}