下面列出了org.hibernate.persister.collection.CollectionPropertyNames#org.hibernate.persister.collection.QueryableCollection 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ReactiveDynamicBatchingCollectionDelegator(
QueryableCollection collectionPersister,
int maxBatchSize,
SessionFactoryImplementor factory,
LoadQueryInfluencers influencers) {
super( collectionPersister, factory, influencers );
this.maxBatchSize = maxBatchSize;
if ( collectionPersister.isOneToMany() ) {
this.singleKeyLoader = new ReactiveOneToManyLoader( collectionPersister, 1, factory, influencers );
}
else {
throw new UnsupportedOperationException();
// this.singleKeyLoader = new ReactiveBasicCollectionLoader( collectionPersister, 1, factory, influencers );
}
this.batchLoader = new ReactiveDynamicBatchingCollectionInitializer( collectionPersister, factory, influencers );
}
protected QueryableCollection getQueryableCollection(
String entityName,
String propertyName,
SessionFactoryImplementor factory) throws HibernateException {
final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister( entityName );
final Type type = ownerMapping.toType( propertyName );
if ( !type.isCollectionType() ) {
throw new MappingException(
"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
);
}
final String role = ( (CollectionType) type ).getRole();
try {
return (QueryableCollection) factory.getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
public SubselectOneToManyLoader(
QueryableCollection persister,
String subquery,
Collection entityKeys,
QueryParameters queryParameters,
Map namedParameterLocMap,
SessionFactoryImplementor factory,
Map enabledFilters)
throws MappingException {
super(persister, 1, subquery, factory, enabledFilters);
keys = new Serializable[ entityKeys.size() ];
Iterator iter = entityKeys.iterator();
int i=0;
while ( iter.hasNext() ) {
keys[i++] = ( (EntityKey) iter.next() ).getIdentifier();
}
this.namedParameters = queryParameters.getNamedParameters();
this.types = queryParameters.getFilteredPositionalParameterTypes();
this.values = queryParameters.getFilteredPositionalParameterValues();
this.namedParameterLocMap = namedParameterLocMap;
}
public BasicCollectionJoinWalker(
QueryableCollection collectionPersister,
int batchSize,
String subquery,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( factory, loadQueryInfluencers );
this.collectionPersister = collectionPersister;
String alias = generateRootAlias( collectionPersister.getRole() );
walkCollectionTree(collectionPersister, alias);
List allAssociations = new ArrayList();
allAssociations.addAll(associations);
allAssociations.add( OuterJoinableAssociation.createRoot( collectionPersister.getCollectionType(), alias, getFactory() ) );
initPersisters(allAssociations, LockMode.NONE);
initStatementString(alias, batchSize, subquery);
}
public SubselectOneToManyLoader(
QueryableCollection persister,
String subquery,
Collection entityKeys,
QueryParameters queryParameters,
Map<String, int[]> namedParameterLocMap,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, 1, subquery, factory, loadQueryInfluencers );
keys = new Serializable[ entityKeys.size() ];
Iterator iter = entityKeys.iterator();
int i=0;
while ( iter.hasNext() ) {
keys[i++] = ( (EntityKey) iter.next() ).getIdentifier();
}
this.namedParameters = queryParameters.getNamedParameters();
this.types = queryParameters.getFilteredPositionalParameterTypes();
this.values = queryParameters.getFilteredPositionalParameterValues();
this.namedParameterLocMap = namedParameterLocMap;
}
public void prepareForDot(String propertyName) throws SemanticException {
FromElement fromElement = getFromElement();
if ( fromElement == null ) {
throw new IllegalStateException( "No FROM element for index operator!" );
}
QueryableCollection queryableCollection = fromElement.getQueryableCollection();
if ( queryableCollection != null && !queryableCollection.isOneToMany() ) {
FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild();
String path = collectionNode.getPath() + "[]." + propertyName;
if ( log.isDebugEnabled() ) {
log.debug( "Creating join for many-to-many elements for " + path );
}
FromElementFactory factory = new FromElementFactory( fromElement.getFromClause(), fromElement, path );
// This will add the new from element to the origin.
FromElement elementJoin = factory.createElementJoin( queryableCollection );
setFromElement( elementJoin );
}
}
@Override
public String getAssociatedEntityName(SessionFactoryImplementor factory)
throws MappingException {
try {
QueryableCollection collectionPersister = (QueryableCollection) factory
.getCollectionPersister( role );
if ( !collectionPersister.getElementType().isEntityType() ) {
throw new MappingException(
"collection was not an association: " +
collectionPersister.getRole()
);
}
return collectionPersister.getElementPersister().getEntityName();
}
catch (ClassCastException cce) {
throw new MappingException( "collection role is not queryable " + role );
}
}
public String getAssociatedEntityName(SessionFactoryImplementor factory)
throws MappingException {
try {
QueryableCollection collectionPersister = (QueryableCollection) factory
.getCollectionPersister( role );
if ( !collectionPersister.getElementType().isEntityType() ) {
throw new MappingException(
"collection was not an association: " +
collectionPersister.getRole()
);
}
return collectionPersister.getElementPersister().getEntityName();
}
catch (ClassCastException cce) {
throw new MappingException( "collection role is not queryable " + role );
}
}
public static CollectionInitializer createBatchingCollectionInitializer(
final QueryableCollection persister,
final int maxBatchSize,
final SessionFactoryImplementor factory,
final Map enabledFilters)
throws MappingException {
if ( maxBatchSize>1 ) {
int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
for ( int i=0; i<batchSizesToCreate.length; i++ ) {
loadersToCreate[i] = new BasicCollectionLoader(persister, batchSizesToCreate[i], factory, enabledFilters);
}
return new BatchingCollectionInitializer(persister, batchSizesToCreate, loadersToCreate);
}
else {
return new BasicCollectionLoader(persister, factory, enabledFilters);
}
}
private void determineValueSelectExpressions(QueryableCollection collectionPersister, List selections) {
AliasGenerator aliasGenerator = new LocalAliasGenerator( 1 );
appendSelectExpressions( collectionPersister.getElementColumnNames(), selections, aliasGenerator );
Type valueType = collectionPersister.getElementType();
if ( valueType.isAssociationType() ) {
EntityType valueEntityType = (EntityType) valueType;
Queryable valueEntityPersister = (Queryable) sfi().getEntityPersister(
valueEntityType.getAssociatedEntityName( sfi() )
);
SelectFragment fragment = valueEntityPersister.propertySelectFragmentFragment(
elementTableAlias(),
null,
false
);
appendSelectExpressions( fragment, selections, aliasGenerator );
}
}
protected BasicCollectionLoader(
QueryableCollection collectionPersister,
int batchSize,
String subquery,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( collectionPersister, factory, loadQueryInfluencers );
JoinWalker walker = new BasicCollectionJoinWalker(
collectionPersister,
batchSize,
subquery,
factory,
loadQueryInfluencers
);
initFromWalker( walker );
postInstantiate();
if ( LOG.isDebugEnabled() ) {
LOG.debugf( "Static select for collection %s: %s", collectionPersister.getRole(), getSQLString() );
}
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
String role = criteriaQuery.getEntityName(criteria, propertyName) +
'.' +
criteriaQuery.getPropertyName(propertyName);
QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory()
.getCollectionPersister(role);
//String[] fk = StringHelper.qualify( "collection_", cp.getKeyColumnNames() );
String[] fk = cp.getKeyColumnNames();
String[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames(); //TODO: handle property-ref
return "? " +
op +
" (select count(*) from " +
cp.getTableName() +
//" collection_ where " +
" where " +
new ConditionFragment()
.setTableAlias( criteriaQuery.getSQLAlias(criteria, propertyName) )
.setCondition(pk, fk)
.toFragmentString() +
")";
}
public void addManyToManyJoin(JoinFragment outerjoin, QueryableCollection collection) throws MappingException {
String manyToManyFilter = collection.getManyToManyFilterFragment( rhsAlias, enabledFilters );
String condition = "".equals( manyToManyFilter )
? on
: "".equals( on )
? manyToManyFilter
: on + " and " + manyToManyFilter;
outerjoin.addJoin(
joinable.getTableName(),
rhsAlias,
lhsColumns,
rhsColumns,
joinType,
condition
);
outerjoin.addJoins(
joinable.fromJoinFragment( rhsAlias, false, true ),
joinable.whereJoinFragment( rhsAlias, false, true )
);
}
public String[] toAliasedColumns(String alias, String propertyName) {
final QueryableCollection queryableCollection = (QueryableCollection) persister;
if ( propertyName.equals( CollectionPropertyNames.COLLECTION_ELEMENTS ) ) {
return queryableCollection.getElementColumnNames( alias );
}
else if ( propertyName.equals( CollectionPropertyNames.COLLECTION_INDICES ) ) {
return queryableCollection.getIndexColumnNames( alias );
}
else {
throw new IllegalArgumentException(
String.format(
"Collection propertyName must be either %s or %s; instead it was %s.",
CollectionPropertyNames.COLLECTION_ELEMENTS,
CollectionPropertyNames.COLLECTION_INDICES,
propertyName
)
);
}
}
/**
* Locate the collection persister by the collection role, requiring that
* such a persister exist.
*
* @param role The collection role name.
*
* @return The defined CollectionPersister for this collection role.
*
* @throws QueryException Indicates that the collection persister could not be found.
*/
public QueryableCollection requireQueryableCollection(String role) throws QueryException {
try {
QueryableCollection queryableCollection = (QueryableCollection) sfi.getMetamodel().collectionPersister( role );
if ( queryableCollection != null ) {
collectionPropertyMappingByRole.put( role, new CollectionPropertyMapping( queryableCollection ) );
}
return queryableCollection;
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
private void visitCollectionElements(CollectionDefinition collectionDefinition) {
final CollectionElementDefinition elementDefinition = collectionDefinition.getElementDefinition();
strategy.startingCollectionElements( elementDefinition );
final Type collectionElementType = elementDefinition.getType();
if ( collectionElementType.isAnyType() ) {
visitAnyDefinition( elementDefinition.toAnyMappingDefinition() );
}
else if ( collectionElementType.isComponentType() ) {
visitCompositeDefinition( elementDefinition.toCompositeElementDefinition() );
}
else if ( collectionElementType.isEntityType() ) {
if ( ! collectionDefinition.getCollectionPersister().isOneToMany() ) {
final QueryableCollection queryableCollection = (QueryableCollection) collectionDefinition.getCollectionPersister();
addAssociationKey(
new AssociationKey(
queryableCollection.getTableName(),
queryableCollection.getElementColumnNames()
)
);
}
visitEntityDefinition( elementDefinition.toEntityDefinition() );
}
strategy.finishingCollectionElements( elementDefinition );
}
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
String entityName = criteriaQuery.getEntityName( criteria, propertyName );
String actualPropertyName = criteriaQuery.getPropertyName( propertyName );
String sqlAlias = criteriaQuery.getSQLAlias( criteria, propertyName );
SessionFactoryImplementor factory = criteriaQuery.getFactory();
QueryableCollection collectionPersister = getQueryableCollection( entityName, actualPropertyName, factory );
String[] collectionKeys = collectionPersister.getKeyColumnNames();
String[] ownerKeys = ( ( Loadable ) factory.getEntityPersister( entityName ) ).getIdentifierColumnNames();
String innerSelect = "(select 1 from " + collectionPersister.getTableName()
+ " where "
+ new ConditionFragment().setTableAlias( sqlAlias ).setCondition( ownerKeys, collectionKeys ).toFragmentString()
+ ")";
return excludeEmpty()
? "exists " + innerSelect
: "not exists " + innerSelect;
}
public ReactiveCollectionLoader(
QueryableCollection collectionPersister,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) {
super(collectionPersister, factory, loadQueryInfluencers);
this.reactiveResultSetProcessor = new ReactiveLoaderBasedResultSetProcessor( this );
}
/**
* Used for collection filters
*/
private void addFromAssociation(final String elementName, final String collectionRole)
throws QueryException {
//q.addCollection(collectionName, collectionRole);
QueryableCollection persister = getCollectionPersister( collectionRole );
Type collectionElementType = persister.getElementType();
if ( !collectionElementType.isEntityType() ) {
throw new QueryException( "collection of values in filter: " + elementName );
}
String[] keyColumnNames = persister.getKeyColumnNames();
//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
String collectionName;
JoinSequence join = new JoinSequence( getFactory() );
collectionName = persister.isOneToMany() ?
elementName :
createNameForCollection( collectionRole );
join.setRoot( persister, collectionName );
if ( !persister.isOneToMany() ) {
//many-to-many
addCollection( collectionName, collectionRole );
try {
join.addJoin( ( AssociationType ) persister.getElementType(),
elementName,
JoinFragment.INNER_JOIN,
persister.getElementColumnNames(collectionName) );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
}
join.addCondition( collectionName, keyColumnNames, " = ?" );
//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
EntityType elemType = ( EntityType ) collectionElementType;
addFrom( elementName, elemType.getAssociatedEntityName(), join );
}
@Override
public ReactiveCollectionLoader createRealBatchingCollectionInitializer(
QueryableCollection persister,
int maxBatchSize,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
int[] batchSizes = ArrayHelper.getBatchSizes( maxBatchSize );
ReactiveCollectionLoader[] loaders = new ReactiveCollectionLoader[ batchSizes.length ];
for ( int i = 0; i < batchSizes.length; i++ ) {
throw new UnsupportedOperationException();
// loaders[i] = new ReactiveBasicCollectionLoader( persister, batchSizes[i], factory, loadQueryInfluencers );
}
return new ReactivePaddedBatchingCollectionInitializer( persister, batchSizes, loaders, factory, loadQueryInfluencers);
}
private boolean isManyToManyRoot(Joinable joinable) {
if ( joinable != null && joinable.isCollection() ) {
QueryableCollection persister = ( QueryableCollection ) joinable;
return persister.isManyToMany();
}
return false;
}
/**
* Builds a batch-fetch capable CollectionInitializer for basic and many-to-many collections (collections with
* a dedicated collection table).
*
* @param persister THe collection persister
* @param maxBatchSize The maximum number of keys to batch-fetch together
* @param factory The SessionFactory
* @param influencers Any influencers that should affect the built query
*
* @return The batch-fetch capable collection initializer
*/
public ReactiveCollectionLoader createBatchingCollectionInitializer(
QueryableCollection persister,
int maxBatchSize,
SessionFactoryImplementor factory,
LoadQueryInfluencers influencers) {
if ( maxBatchSize <= 1 ) {
// no batching
return buildNonBatchingLoader( persister, factory, influencers );
}
return createRealBatchingCollectionInitializer( persister, maxBatchSize, factory, influencers );
}
private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
final String aliasToUse = getTableAlias();
Queryable queryable = getQueryable();
if ( queryable == null ) {
QueryableCollection collection = getQueryableCollection();
if ( ! collection.getElementType().isEntityType() ) {
throw new QueryException( "type discrimination cannot be applied to value collection [" + collection.getRole() + "]" );
}
queryable = (Queryable) collection.getElementPersister();
}
handlePropertyBeingDereferenced( getDataType(), DISCRIMINATOR_PROPERTY_NAME );
return new TypeDiscriminatorMetadataImpl( queryable.getTypeDiscriminatorMetadata(), aliasToUse );
}
/**
* Builds a batch-fetch capable CollectionInitializer for one-to-many collections (collections without
* a dedicated collection table).
*
* @param persister THe collection persister
* @param maxBatchSize The maximum number of keys to batch-fetch together
* @param factory The SessionFactory
* @param influencers Any influencers that should affect the built query
*
* @return The batch-fetch capable collection initializer
*/
public ReactiveCollectionLoader createBatchingOneToManyInitializer(
QueryableCollection persister,
int maxBatchSize,
SessionFactoryImplementor factory,
LoadQueryInfluencers influencers) {
if ( maxBatchSize <= 1 ) {
// no batching
return buildNonBatchingLoader( persister, factory, influencers );
}
return createRealBatchingOneToManyInitializer( persister, maxBatchSize, factory, influencers );
}
private boolean isManyToManyRoot(Joinable joinable) {
if ( joinable != null && joinable.isCollection() ) {
QueryableCollection persister = ( QueryableCollection ) joinable;
return persister.isManyToMany();
}
return false;
}
FromElement createCollectionElementsJoin(
QueryableCollection queryableCollection,
String collectionName) throws SemanticException {
JoinSequence collectionJoinSequence = fromClause.getSessionFactoryHelper()
.createCollectionJoinSequence( queryableCollection, collectionName );
this.queryableCollection = queryableCollection;
return createCollectionJoin( collectionJoinSequence, null );
}
public void resolveIndex(AST parent) throws SemanticException {
// An ident node can represent an index expression if the ident
// represents a naked property ref
// *Note: this makes the assumption (which is currently the case
// in the hql-sql grammar) that the ident is first resolved
// itself (addrExpr -> resolve()). The other option, if that
// changes, is to call resolve from here; but it is
// currently un-needed overhead.
if (!(isResolved() && nakedPropertyRef)) {
throw new UnsupportedOperationException();
}
String propertyName = getOriginalText();
if (!getDataType().isCollectionType()) {
throw new SemanticException("Collection expected; [" + propertyName + "] does not refer to a collection property");
}
// TODO : most of below was taken verbatim from DotNode; should either delegate this logic or super-type it
CollectionType type = (CollectionType) getDataType();
String role = type.getRole();
QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);
String alias = null; // DotNode uses null here...
String columnTableAlias = getFromElement().getTableAlias();
JoinType joinType = JoinType.INNER_JOIN;
boolean fetch = false;
FromElementFactory factory = new FromElementFactory(
getWalker().getCurrentFromClause(),
getFromElement(),
propertyName,
alias,
getFromElement().toColumns(columnTableAlias, propertyName, false),
true
);
FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, true);
setFromElement(elem);
getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces()); // Always add the collection's query spaces.
}
public ReactiveOneToManyLoader(
QueryableCollection oneToManyPersister,
int batchSize,
SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
this(oneToManyPersister, batchSize, null, factory, loadQueryInfluencers);
}
private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
QueryableCollection collPersister = q.getCollectionPersister( collectionRole );
if ( !collPersister.hasIndex() ) throw new QueryException( "unindexed collection before []: " + path );
String[] indexCols = collPersister.getIndexColumnNames();
if ( indexCols.length != 1 ) throw new QueryException( "composite-index appears in []: " + path );
//String[] keyCols = collPersister.getKeyColumnNames();
JoinSequence fromJoins = new JoinSequence( q.getFactory() )
.setUseThetaStyle( useThetaStyleJoin )
.setRoot( collPersister, collectionName )
.setNext( joinSequence.copy() );
if ( !continuation ) addJoin( collectionName, collPersister.getCollectionType() );
joinSequence.addCondition( collectionName + '.' + indexCols[0] + " = " ); //TODO: get SQL rendering out of here
CollectionElement elem = new CollectionElement();
elem.elementColumns = collPersister.getElementColumnNames(collectionName);
elem.elementType = collPersister.getElementType();
elem.isOneToMany = collPersister.isOneToMany();
elem.alias = collectionName;
elem.joinSequence = joinSequence;
collectionElements.addLast( elem );
setExpectingCollectionIndex();
q.addCollection( collectionName, collectionRole );
q.addFromJoinOnly( collectionName, fromJoins );
}
public static BatchingCollectionInitializer createBatchingOneToManyInitializer(
final QueryableCollection persister,
final int maxBatchSize,
final SessionFactoryImplementor factory,
final LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize > DEFAULT_MAX_BATCH_SIZE ? maxBatchSize : DEFAULT_MAX_BATCH_SIZE);
Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
for ( int i=0; i<batchSizesToCreate.length; i++ ) {
loadersToCreate[i] = new OneToManyLoader( persister, batchSizesToCreate[i], factory, loadQueryInfluencers );
}
return new BatchingCollectionInitializer( persister, batchSizesToCreate, loadersToCreate, maxBatchSize );
}