HHH-13011 : Add option enabling/disabling use of an entity's mapped where-clause when loading collections of that entity; change to disable by default
(cherry picked from commit 4735c2d5aa
)
This commit is contained in:
parent
b8b7f16d14
commit
101b22f343
|
@ -91,10 +91,12 @@ import org.hibernate.boot.spi.InFlightMetadataCollector;
|
|||
import org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref;
|
||||
import org.hibernate.boot.spi.MetadataBuildingContext;
|
||||
import org.hibernate.boot.spi.NaturalIdUniqueKeyBinder;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.FkSecondPass;
|
||||
import org.hibernate.cfg.SecondPass;
|
||||
import org.hibernate.engine.FetchStyle;
|
||||
import org.hibernate.engine.FetchTiming;
|
||||
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.engine.spi.FilterDefinition;
|
||||
import org.hibernate.id.PersistentIdentifierGenerator;
|
||||
|
@ -104,6 +106,7 @@ import org.hibernate.internal.log.DeprecationLogger;
|
|||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.internal.util.compare.EqualsHelper;
|
||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||
import org.hibernate.loader.PropertyPath;
|
||||
import org.hibernate.mapping.Any;
|
||||
import org.hibernate.mapping.Array;
|
||||
|
@ -3478,18 +3481,25 @@ public class ModelBinder {
|
|||
|
||||
final PersistentClass referencedEntityBinding = mappingDocument.getMetadataCollector()
|
||||
.getEntityBinding( elementSource.getReferencedEntityName() );
|
||||
// For a one-to-many association, there are 2 possible sources of "where" clauses that apply
|
||||
// to the associated entity table:
|
||||
// 1) from the associated entity mapping; i.e., <class name="..." ... where="..." .../>
|
||||
// 2) from the collection mapping; e.g., <set name="..." ... where="..." .../>
|
||||
// Collection#setWhere is used to set the "where" clause that applies to the collection table
|
||||
// (which is the associated entity table for a one-to-many association).
|
||||
collectionBinding.setWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
getPluralAttributeSource().getWhere()
|
||||
)
|
||||
);
|
||||
|
||||
if ( useEntityWhereClauseForCollections() ) {
|
||||
// For a one-to-many association, there are 2 possible sources of "where" clauses that apply
|
||||
// to the associated entity table:
|
||||
// 1) from the associated entity mapping; i.e., <class name="..." ... where="..." .../>
|
||||
// 2) from the collection mapping; e.g., <set name="..." ... where="..." .../>
|
||||
// Collection#setWhere is used to set the "where" clause that applies to the collection table
|
||||
// (which is the associated entity table for a one-to-many association).
|
||||
collectionBinding.setWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
getPluralAttributeSource().getWhere()
|
||||
)
|
||||
);
|
||||
}
|
||||
else {
|
||||
// ignore entity's where clause
|
||||
collectionBinding.setWhere( getPluralAttributeSource().getWhere() );
|
||||
}
|
||||
|
||||
elementBinding.setReferencedEntityName( referencedEntityBinding.getEntityName() );
|
||||
elementBinding.setAssociatedClass( referencedEntityBinding );
|
||||
|
@ -3623,18 +3633,26 @@ public class ModelBinder {
|
|||
// (which is the join table for a many-to-many association).
|
||||
// This "where" clause comes from the collection mapping; e.g., <set name="..." ... where="..." .../>
|
||||
getCollectionBinding().setWhere( getPluralAttributeSource().getWhere() );
|
||||
// For a many-to-many association, there are 2 possible sources of "where" clauses that apply
|
||||
// to the associated entity table (not the join table):
|
||||
// 1) from the associated entity mapping; i.e., <class name="..." ... where="..." .../>
|
||||
// 2) from the many-to-many mapping; i.e <many-to-many ... where="...".../>
|
||||
// Collection#setManytoManyWhere is used to set the "where" clause that applies to
|
||||
// to the many-to-many associated entity table (not the join table).
|
||||
getCollectionBinding().setManyToManyWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
elementSource.getWhere()
|
||||
)
|
||||
);
|
||||
|
||||
if ( useEntityWhereClauseForCollections() ) {
|
||||
// For a many-to-many association, there are 2 possible sources of "where" clauses that apply
|
||||
// to the associated entity table (not the join table):
|
||||
// 1) from the associated entity mapping; i.e., <class name="..." ... where="..." .../>
|
||||
// 2) from the many-to-many mapping; i.e <many-to-many ... where="...".../>
|
||||
// Collection#setManytoManyWhere is used to set the "where" clause that applies to
|
||||
// to the many-to-many associated entity table (not the join table).
|
||||
getCollectionBinding().setManyToManyWhere(
|
||||
StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty(
|
||||
referencedEntityBinding.getWhere(),
|
||||
elementSource.getWhere()
|
||||
)
|
||||
);
|
||||
}
|
||||
else {
|
||||
// ignore entity's where clause
|
||||
getCollectionBinding().setManyToManyWhere( elementSource.getWhere() );
|
||||
}
|
||||
|
||||
getCollectionBinding().setManyToManyOrdering( elementSource.getOrder() );
|
||||
|
||||
if ( !CollectionHelper.isEmpty( elementSource.getFilterSources() )
|
||||
|
@ -3716,6 +3734,18 @@ public class ModelBinder {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean useEntityWhereClauseForCollections() {
|
||||
return ConfigurationHelper.getBoolean(
|
||||
AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
metadataBuildingContext
|
||||
.getBuildingOptions()
|
||||
.getServiceRegistry()
|
||||
.getService( ConfigurationService.class )
|
||||
.getSettings(),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private class PluralAttributeListSecondPass extends AbstractPluralAttributeSecondPass {
|
||||
public PluralAttributeListSecondPass(
|
||||
MappingDocument sourceDocument,
|
||||
|
|
|
@ -1130,6 +1130,22 @@ public interface AvailableSettings {
|
|||
*/
|
||||
String CUSTOM_ENTITY_DIRTINESS_STRATEGY = "hibernate.entity_dirtiness_strategy";
|
||||
|
||||
/**
|
||||
* Controls whether an entity's "where" clause, mapped using <code>@Where(clause="....")</code>
|
||||
* or <code><entity ... where="..."></code>, is taken into account when loading one-to-many
|
||||
* or many-to-many collections of that type of entity.
|
||||
* <p/>
|
||||
* This setting has no affect on collections of embeddable values containing an association to
|
||||
* that type of entity.
|
||||
* <p/>
|
||||
* When `true`, the entity's "where" clause will be taken into account when loading
|
||||
* one-to-many or many-to-many collections of that type of entity.
|
||||
* <p/>
|
||||
* `false` (the default) indicates that the entity's "where" clause will be ignored when loading one-to-many or
|
||||
* many-to-many collections of that type of entity.
|
||||
*/
|
||||
String USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = "hibernate.use_entity_where_clause_for_collections";
|
||||
|
||||
/**
|
||||
* Strategy for multi-tenancy.
|
||||
|
||||
|
|
|
@ -69,6 +69,7 @@ import org.hibernate.boot.spi.MetadataBuildingContext;
|
|||
import org.hibernate.cfg.AccessType;
|
||||
import org.hibernate.cfg.AnnotatedClassType;
|
||||
import org.hibernate.cfg.AnnotationBinder;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.BinderHelper;
|
||||
import org.hibernate.cfg.CollectionPropertyHolder;
|
||||
import org.hibernate.cfg.CollectionSecondPass;
|
||||
|
@ -83,9 +84,11 @@ import org.hibernate.cfg.PropertyInferredData;
|
|||
import org.hibernate.cfg.PropertyPreloadedData;
|
||||
import org.hibernate.cfg.SecondPass;
|
||||
import org.hibernate.criterion.Junction;
|
||||
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||
import org.hibernate.mapping.Any;
|
||||
import org.hibernate.mapping.Backref;
|
||||
import org.hibernate.mapping.Collection;
|
||||
|
@ -945,13 +948,24 @@ public abstract class CollectionBinder {
|
|||
}
|
||||
}
|
||||
|
||||
final boolean useEntityWhereClauseForCollections = ConfigurationHelper.getBoolean(
|
||||
AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
buildingContext
|
||||
.getBuildingOptions()
|
||||
.getServiceRegistry()
|
||||
.getService( ConfigurationService.class )
|
||||
.getSettings(),
|
||||
false
|
||||
);
|
||||
|
||||
// There are 2 possible sources of "where" clauses that apply to the associated entity table:
|
||||
// 1) from the associated entity mapping; i.e., @Entity @Where(clause="...")
|
||||
// (ignored if useEntityWhereClauseForCollections == false)
|
||||
// 2) from the collection mapping;
|
||||
// for one-to-many, e.g., @OneToMany @JoinColumn @Where(clause="...") public Set<Rating> getRatings();
|
||||
// for many-to-many e.g., @ManyToMany @Where(clause="...") public Set<Rating> getRatings();
|
||||
String whereOnClassClause = null;
|
||||
if ( property.getElementClass() != null ) {
|
||||
if ( useEntityWhereClauseForCollections && property.getElementClass() != null ) {
|
||||
Where whereOnClass = property.getElementClass().getAnnotation( Where.class );
|
||||
if ( whereOnClass != null ) {
|
||||
whereOnClassClause = whereOnClass.clause();
|
||||
|
|
Loading…
Reference in New Issue