org.hibernate.engine.spi.NamedSQLQueryDefinition Java Examples

The following examples show how to use org.hibernate.engine.spi.NamedSQLQueryDefinition. 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
Source File: ReactiveSessionImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
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
Source File: BasicHibernateDao.java    From base-framework with 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 #3
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #4
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #6
Source File: NamedQueryRepository.java    From lams with 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 #7
Source File: NamedQueryRepository.java    From lams with 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 #8
Source File: NamedQueryRepository.java    From lams with 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 #9
Source File: ReactiveSessionImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #10
Source File: NamedQueryRepository.java    From lams with 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 #11
Source File: Configuration.java    From lams with 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 #12
Source File: NativeQueryImpl.java    From lams with 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 File: SessionFactoryImpl.java    From lams with 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
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private NativeQueryImplementor createNativeQuery(NamedSQLQueryDefinition queryDefinition, boolean isOrdinalParameterZeroBased) {
	final ParameterMetadata parameterMetadata = factory.getQueryPlanCache().getSQLParameterMetadata(
			queryDefinition.getQueryString(),
			isOrdinalParameterZeroBased
	);
	return getNativeQueryImplementor( queryDefinition, parameterMetadata );
}
 
Example #15
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #17
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #18
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(String name) {
	return namedNativeQueryMap.get( name );
}
 
Example #19
Source File: ReactiveNativeQueryImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ReactiveNativeQueryImpl(
		NamedSQLQueryDefinition queryDef,
		SharedSessionContractImplementor session,
		ParameterMetadata parameterMetadata) {
	super(queryDef, session, parameterMetadata);
}
 
Example #20
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public java.util.Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
	return namedNativeQueryMap.values();
}
 
Example #21
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void applyNamedNativeQuery(String name, NamedSQLQueryDefinition query) {
	checkQueryName( name );
	namedNativeQueryMap.put( name.intern(), query );
}
 
Example #22
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void addDefaultNamedNativeQuery(NamedSQLQueryDefinition query) {
	applyNamedNativeQuery( query.getName(), query );
	defaultNamedNativeQueryNames.add( query.getName() );
}
 
Example #23
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
MetadataImpl(
		UUID uuid,
		MetadataBuildingOptions metadataBuildingOptions,
		MutableIdentifierGeneratorFactory identifierGeneratorFactory,
		Map<String, PersistentClass> entityBindingMap,
		Map<Class, MappedSuperclass> mappedSuperclassMap,
		Map<String, Collection> collectionBindingMap,
		Map<String, TypeDefinition> typeDefinitionMap,
		Map<String, FilterDefinition> filterDefinitionMap,
		Map<String, FetchProfile> fetchProfileMap,
		Map<String, String> imports,
		Map<String, IdentifierGeneratorDefinition> idGeneratorDefinitionMap,
		Map<String, NamedQueryDefinition> namedQueryMap,
		Map<String, NamedSQLQueryDefinition> namedNativeQueryMap,
		Map<String, NamedProcedureCallDefinition> namedProcedureCallMap,
		Map<String, ResultSetMappingDefinition> sqlResultSetMappingMap,
		Map<String, NamedEntityGraphDefinition> namedEntityGraphMap,
		Map<String, SQLFunction> sqlFunctionMap,
		java.util.Collection<DomainDataRegionConfigImpl.Builder> cacheRegionConfigBuilders,
		Database database,
		BootstrapContext bootstrapContext) {
	this.uuid = uuid;
	this.metadataBuildingOptions = metadataBuildingOptions;
	this.identifierGeneratorFactory = identifierGeneratorFactory;
	this.entityBindingMap = entityBindingMap;
	this.mappedSuperclassMap = mappedSuperclassMap;
	this.collectionBindingMap = collectionBindingMap;
	this.typeDefinitionMap = typeDefinitionMap;
	this.filterDefinitionMap = filterDefinitionMap;
	this.fetchProfileMap = fetchProfileMap;
	this.imports = imports;
	this.idGeneratorDefinitionMap = idGeneratorDefinitionMap;
	this.namedQueryMap = namedQueryMap;
	this.namedNativeQueryMap = namedNativeQueryMap;
	this.namedProcedureCallMap = namedProcedureCallMap;
	this.sqlResultSetMappingMap = sqlResultSetMappingMap;
	this.namedEntityGraphMap = namedEntityGraphMap;
	this.sqlFunctionMap = sqlFunctionMap;
	this.cacheRegionConfigBuilders = cacheRegionConfigBuilders;
	this.database = database;
	this.bootstrapContext = bootstrapContext;
}
 
Example #24
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(String name) {
	return namedNativeQueryMap.get( name );
}
 
Example #25
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public java.util.Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
	return namedNativeQueryMap.values();
}
 
Example #26
Source File: SessionFactoryWrapper.java    From lemon with Apache License 2.0 4 votes vote down vote up
public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) {
    return sessionFactoryImplementor.getNamedSQLQuery(queryName);
}
 
Example #27
Source File: SessionFactoryWrapper.java    From lemon with Apache License 2.0 4 votes vote down vote up
public void registerNamedSQLQueryDefinition(String name,
        NamedSQLQueryDefinition definition) {
    sessionFactoryImplementor.registerNamedSQLQueryDefinition(name,
            definition);
}
 
Example #28
Source File: NamedQueryBinder.java    From lams with 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 #29
Source File: AbstractDelegatingMetadata.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Collection<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() {
	return delegate.getNamedNativeQueryDefinitions();
}
 
Example #30
Source File: AbstractDelegatingMetadata.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public NamedSQLQueryDefinition getNamedNativeQueryDefinition(String name) {
	return delegate.getNamedNativeQueryDefinition( name );
}