/** * Constructs a SQLQueryImpl given a sql query defined in the mappings. * * @param queryDef The representation of the defined <sql-query/>. * @param session The session to which this SQLQueryImpl belongs. * @param parameterMetadata Metadata about parameters found in the query. */ SQLQueryImpl(NamedSQLQueryDefinition queryDef, SessionImplementor session, ParameterMetadata parameterMetadata) { super( queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata ); if ( queryDef.getResultSetRef() != null ) { ResultSetMappingDefinition definition = session.getFactory() .getResultSetMapping( queryDef.getResultSetRef() ); if (definition == null) { throw new MappingException( "Unable to find resultset-ref definition: " + queryDef.getResultSetRef() ); } this.queryReturns = new ArrayList<NativeSQLQueryReturn>(Arrays.asList( definition.getQueryReturns() )); } else if ( queryDef.getQueryReturns() != null && queryDef.getQueryReturns().length > 0 ) { this.queryReturns = new ArrayList<NativeSQLQueryReturn>(Arrays.asList( queryDef.getQueryReturns())); } else { this.queryReturns = new ArrayList<NativeSQLQueryReturn>(); } this.querySpaces = queryDef.getQuerySpaces(); this.callable = queryDef.isCallable(); }
@Override public Query getNamedQuery(String queryName) throws MappingException { errorIfClosed(); NamedQueryDefinition nqd = factory.getNamedQuery( queryName ); final Query query; if ( nqd != null ) { query = createQuery( nqd ); } else { NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery( queryName ); if ( nsqlqd==null ) { throw new MappingException( "Named query not known: " + queryName ); } query = createSQLQuery( nsqlqd ); nqd = nsqlqd; } initQuery( query, nqd ); return query; }
@Override public Query getNamedSQLQuery(String queryName) throws MappingException { errorIfClosed(); NamedSQLQueryDefinition nsqlqd = factory.getNamedSQLQuery( queryName ); if ( nsqlqd==null ) { throw new MappingException( "Named SQL query not known: " + queryName ); } Query query = new SQLQueryImpl( nsqlqd, this, factory.getQueryPlanCache().getSQLParameterMetadata( nsqlqd.getQueryString() ) ); query.setComment( "named native SQL query " + queryName ); initQuery( query, nsqlqd ); return query; }
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 ); }
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 ); }
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 ); }
/** * 根据查询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); }
@Override public void addNamedNativeQuery(NamedSQLQueryDefinition def) { if ( def == null || def.getName() == null ) { throw new IllegalArgumentException( "Named native query definition object or name is null: " + def.getQueryString() ); } namedNativeQueryDefs.put( def.getName(), def ); }
@Override public SQLQuery createSQLQuery(NamedSQLQueryDefinition namedQueryDefinition) { final ParameterMetadata parameterMetadata = factory.getQueryPlanCache().getSQLParameterMetadata( namedQueryDefinition.getQueryString() ); final SQLQuery query = new SQLQueryImpl( namedQueryDefinition, this, parameterMetadata ); query.setComment( "named native SQL query " + namedQueryDefinition.getName() ); return query; }
public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) { return sessionFactoryImplementor.getNamedSQLQuery(queryName); }
public void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) { sessionFactoryImplementor.registerNamedSQLQueryDefinition(name, definition); }
protected void reset() { metadataSourceQueue = new MetadataSourceQueue(); createReflectionManager(); classes = new HashMap<String,PersistentClass>(); imports = new HashMap<String,String>(); collections = new HashMap<String,Collection>(); tables = new TreeMap<String,Table>(); 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>( ); typeDefs = new HashMap<String,TypeDef>(); filterDefinitions = new HashMap<String, FilterDefinition>(); fetchProfiles = new HashMap<String, FetchProfile>(); auxiliaryDatabaseObjects = new ArrayList<AuxiliaryDatabaseObject>(); tableNameBinding = new HashMap(); columnNameBindingPerTable = new HashMap(); secondPasses = new ArrayList<SecondPass>(); propertyReferences = new ArrayList<Mappings.PropertyReference>(); extendsQueue = new HashMap<ExtendsQueueEntry, String>(); xmlHelper = new XMLHelper(); interceptor = EmptyInterceptor.INSTANCE; properties = Environment.getProperties(); entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER; sqlFunctions = new HashMap<String, SQLFunction>(); entityTuplizerFactory = new EntityTuplizerFactory(); // componentTuplizerFactory = new ComponentTuplizerFactory(); identifierGeneratorFactory = new DefaultIdentifierGeneratorFactory(); mappedSuperClasses = new HashMap<Class<?>, MappedSuperclass>(); metadataSourcePrecedence = Collections.emptyList(); namedGenerators = new HashMap<String, IdGenerator>(); joins = new HashMap<String, Map<String, Join>>(); classTypes = new HashMap<String, AnnotatedClassType>(); generatorTables = new HashMap<String, Properties>(); defaultNamedQueryNames = new HashSet<String>(); defaultNamedNativeQueryNames = new HashSet<String>(); defaultSqlResultSetMappingNames = new HashSet<String>(); defaultNamedProcedure = new HashSet<String>( ); defaultNamedGenerators = new HashSet<String>(); uniqueConstraintHoldersByTable = new HashMap<Table, List<UniqueConstraintHolder>>(); jpaIndexHoldersByTable = new HashMap<Table,List<JPAIndexHolder>>( ); mappedByResolver = new HashMap<String, String>(); propertyRefResolver = new HashMap<String, String>(); caches = new ArrayList<CacheHolder>(); namingStrategyDelegator = LegacyNamingStrategyDelegator.DEFAULT_INSTANCE; setEntityResolver( new EJB3DTDEntityResolver() ); anyMetaDefs = new HashMap<String, AnyMetaDef>(); propertiesAnnotatedWithMapsId = new HashMap<XClass, Map<String, PropertyData>>(); propertiesAnnotatedWithIdAndToOne = new HashMap<XClass, Map<String, PropertyData>>(); specjProprietarySyntaxEnabled = System.getProperty( "hibernate.enable_specj_proprietary_syntax" ) != null; }
public NamedSQLQueryDefinition getSQLQuery(String name) { return namedSqlQueries.get( name ); }
public void addSQLQuery(String name, NamedSQLQueryDefinition query) throws DuplicateMappingException { if ( !defaultNamedNativeQueryNames.contains( name ) ) { applySQLQuery( name, query ); } }
private void applySQLQuery(String name, NamedSQLQueryDefinition query) throws DuplicateMappingException { checkQueryName( name ); namedSqlQueries.put( name.intern(), query ); }
public void addDefaultSQLQuery(String name, NamedSQLQueryDefinition query) { applySQLQuery( name, query ); defaultNamedNativeQueryNames.add( name ); }
public void doSecondPass(Map persistentClasses) throws MappingException { String queryName = queryElem.attribute( "name" ).getValue(); if (path!=null) queryName = path + '.' + queryName; boolean cacheable = "true".equals( queryElem.attributeValue( "cacheable" ) ); String region = queryElem.attributeValue( "cache-region" ); Attribute tAtt = queryElem.attribute( "timeout" ); Integer timeout = tAtt == null ? null : Integer.valueOf( tAtt.getValue() ); Attribute fsAtt = queryElem.attribute( "fetch-size" ); Integer fetchSize = fsAtt == null ? null : Integer.valueOf( fsAtt.getValue() ); Attribute roAttr = queryElem.attribute( "read-only" ); boolean readOnly = roAttr != null && "true".equals( roAttr.getValue() ); Attribute cacheModeAtt = queryElem.attribute( "cache-mode" ); String cacheMode = cacheModeAtt == null ? null : cacheModeAtt.getValue(); Attribute cmAtt = queryElem.attribute( "comment" ); String comment = cmAtt == null ? null : cmAtt.getValue(); java.util.List<String> synchronizedTables = new ArrayList<String>(); Iterator tables = queryElem.elementIterator( "synchronize" ); while ( tables.hasNext() ) { synchronizedTables.add( ( (Element) tables.next() ).attributeValue( "table" ) ); } boolean callable = "true".equals( queryElem.attributeValue( "callable" ) ); NamedSQLQueryDefinition namedQuery; Attribute ref = queryElem.attribute( "resultset-ref" ); String resultSetRef = ref == null ? null : ref.getValue(); if ( StringHelper.isNotEmpty( resultSetRef ) ) { namedQuery = new NamedSQLQueryDefinitionBuilder().setName( queryName ) .setQuery( queryElem.getText() ) .setResultSetRef( resultSetRef ) .setQuerySpaces( synchronizedTables ) .setCacheable( cacheable ) .setCacheRegion( region ) .setTimeout( timeout ) .setFetchSize( fetchSize ) .setFlushMode( FlushMode.interpretExternalSetting( queryElem.attributeValue( "flush-mode" ) ) ) .setCacheMode( CacheMode.interpretExternalSetting( cacheMode ) ) .setReadOnly( readOnly ) .setComment( comment ) .setParameterTypes( HbmBinder.getParameterTypes( queryElem ) ) .setCallable( callable ) .createNamedQueryDefinition(); //TODO check there is no actual definition elemnents when a ref is defined } else { ResultSetMappingDefinition definition = buildResultSetMappingDefinition( queryElem, path, mappings ); namedQuery = new NamedSQLQueryDefinitionBuilder().setName( queryName ) .setQuery( queryElem.getText() ) .setQueryReturns( definition.getQueryReturns() ) .setQuerySpaces( synchronizedTables ) .setCacheable( cacheable ) .setCacheRegion( region ) .setTimeout( timeout ) .setFetchSize( fetchSize ) .setFlushMode( FlushMode.interpretExternalSetting( queryElem.attributeValue( "flush-mode" ) ) ) .setCacheMode( CacheMode.interpretExternalSetting( cacheMode ) ) .setReadOnly( readOnly ) .setComment( comment ) .setParameterTypes( HbmBinder.getParameterTypes( queryElem ) ) .setCallable( callable ) .createNamedQueryDefinition(); } if ( LOG.isDebugEnabled() ) { LOG.debugf( "Named SQL query: %s -> %s", namedQuery.getName(), namedQuery.getQueryString() ); } mappings.addSQLQuery( queryName, namedQuery ); }
public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, Mappings mappings) { 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" ); } mappings.addSQLQuery( query.getName(), query ); if ( LOG.isDebugEnabled() ) { LOG.debugf( "Binding named native query: %s => %s", query.getName(), queryAnn.query() ); } }
public NamedSQLQueryDefinition getNamedNativeQuery(String name) { if ( name == null ) { throw new IllegalArgumentException( "null is not a valid native query name" ); } return namedNativeQueryDefs.get( name ); }
@Override public Iterable<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions() { return namedNativeQueryDefs.values(); }
public void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) { namedQueryRepository.registerNamedSQLQueryDefinition( name, definition ); }
public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) { return namedQueryRepository.getNamedSQLQueryDefinition( queryName ); }
public NamedSQLQueryDefinition getNamedSQLQueryDefinition(String queryName) { return namedSqlQueryDefinitionMap.get( queryName ); }
public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) { return namedSqlQueries.get(queryName); }
/** * Get named SQL query metadata. * * @param name The named SQL query name. * @return The meatdata, or null if none found. */ public NamedSQLQueryDefinition getSQLQuery(String name);
/** * Adds metadata for a named SQL query to this repository. * * @param name The name * @param query The metadata * @throws DuplicateMappingException If a query already exists with that name. */ public void addSQLQuery(String name, NamedSQLQueryDefinition query) throws DuplicateMappingException;
public void addDefaultSQLQuery(String name, NamedSQLQueryDefinition query);
public Iterable<NamedSQLQueryDefinition> getNamedNativeQueryDefinitions();
public void addNamedNativeQuery(NamedSQLQueryDefinition def);