HHH-18199 - Remove @Where and @WhereJoinTable

This commit is contained in:
Steve Ebersole 2024-07-24 09:22:16 -05:00
parent 163d48d81c
commit 611844fe79
12 changed files with 53 additions and 928 deletions

View File

@ -173,7 +173,6 @@ import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper
import static org.hibernate.boot.model.internal.EmbeddableBinder.fillEmbeddable;
import static org.hibernate.boot.model.internal.GeneratorBinder.buildGenerators;
import static org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder;
import static org.hibernate.boot.model.source.internal.hbm.ModelBinder.useEntityWhereClauseForCollections;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
import static org.hibernate.internal.util.ReflectHelper.getDefaultSupplier;
import static org.hibernate.internal.util.StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty;
@ -1894,17 +1893,12 @@ public abstract class CollectionBinder {
private String getWhereOnClassClause() {
final TypeDetails elementType = property.getElementType();
if ( elementType != null && useEntityWhereClauseForCollections( buildingContext ) ) {
final SQLRestriction restrictionOnClass = getOverridableAnnotation(
property.getAssociatedType().determineRawClass(),
SQLRestriction.class,
buildingContext
);
return restrictionOnClass != null ? restrictionOnClass.value() : null;
}
else {
return null;
}
final SQLRestriction restrictionOnClass = getOverridableAnnotation(
property.getAssociatedType().determineRawClass(),
SQLRestriction.class,
buildingContext
);
return restrictionOnClass != null ? restrictionOnClass.value() : null;
}
private void addFilterJoinTable(boolean hasAssociationTable, FilterJoinTable filter) {

View File

@ -18,7 +18,6 @@ import java.util.Properties;
import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
import org.hibernate.Remove;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.SourceType;
import org.hibernate.boot.MappingException;
@ -99,10 +98,8 @@ import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.NaturalIdUniqueKeyBinder;
import org.hibernate.boot.spi.SecondPass;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AvailableSettings;
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.generator.internal.GeneratedGeneration;
@ -111,7 +108,6 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.mapping.Any;
import org.hibernate.mapping.Array;
import org.hibernate.mapping.AttributeContainer;
@ -163,7 +159,6 @@ import org.hibernate.usertype.UserType;
import static org.hibernate.boot.model.internal.GeneratorBinder.makeIdGenerator;
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
import static org.hibernate.boot.model.source.internal.hbm.Helper.reflectedPropertyClass;
import static org.hibernate.cfg.AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
import static org.hibernate.internal.util.StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
@ -195,28 +190,6 @@ public class ModelBinder {
this.relationalObjectBinder = new RelationalObjectBinder( context );
}
/**
* @deprecated Interprets the setting {@value AvailableSettings#USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS},
* which itself is deprecated
*/
@SuppressWarnings("removal")
@Remove
@Deprecated( since = "6.2" )
public static boolean useEntityWhereClauseForCollections(MetadataBuildingContext buildingContext) {
final Object explicitSetting = buildingContext
.getBuildingOptions()
.getServiceRegistry()
.requireService( ConfigurationService.class )
.getSettings()
.get( USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS );
if ( explicitSetting != null ) {
DEPRECATION_LOGGER.deprecatedSettingNoReplacement( USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS );
return ConfigurationHelper.toBoolean( explicitSetting, true );
}
return true;
}
public void bindEntityHierarchy(EntityHierarchySourceImpl hierarchySource) {
final RootClass rootEntityDescriptor = new RootClass( hierarchySource.getRootEntityMappingDocument() );
bindRootEntity( hierarchySource, rootEntityDescriptor );
@ -3333,9 +3306,7 @@ public class ModelBinder {
getPluralAttributeSource().getNature()
);
}
if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceBasic ) {
final PluralAttributeElementSourceBasic elementSource =
(PluralAttributeElementSourceBasic) getPluralAttributeSource().getElementSource();
if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceBasic elementSource ) {
final BasicValue elementBinding = new BasicValue(
getMappingDocument(),
getCollectionBinding().getCollectionTable()
@ -3361,9 +3332,7 @@ public class ModelBinder {
// This "where" clause comes from the collection mapping; e.g., <set name="..." ... where="..." .../>
getCollectionBinding().setWhere( getPluralAttributeSource().getWhere() );
}
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceEmbedded ) {
final PluralAttributeElementSourceEmbedded elementSource =
(PluralAttributeElementSourceEmbedded) getPluralAttributeSource().getElementSource();
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceEmbedded elementSource ) {
final Component elementBinding = new Component(
getMappingDocument(),
getCollectionBinding()
@ -3385,9 +3354,7 @@ public class ModelBinder {
// This "where" clause comes from the collection mapping; e.g., <set name="..." ... where="..." .../>
getCollectionBinding().setWhere( getPluralAttributeSource().getWhere() );
}
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceOneToMany ) {
final PluralAttributeElementSourceOneToMany elementSource =
(PluralAttributeElementSourceOneToMany) getPluralAttributeSource().getElementSource();
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceOneToMany elementSource ) {
final OneToMany elementBinding = new OneToMany(
getMappingDocument(),
getCollectionBinding().getOwner()
@ -3396,32 +3363,18 @@ public class ModelBinder {
final PersistentClass referencedEntityBinding = getReferencedEntityBinding( elementSource.getReferencedEntityName() );
if ( useEntityWhereClauseForCollections( metadataBuildingContext ) ) {
// 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(
getNonEmptyOrConjunctionIfBothNonEmpty(
referencedEntityBinding.getWhere(),
getPluralAttributeSource().getWhere()
)
);
}
else {
// ignore entity's where clause
collectionBinding.setWhere( getPluralAttributeSource().getWhere() );
}
collectionBinding.setWhere(
getNonEmptyOrConjunctionIfBothNonEmpty(
referencedEntityBinding.getWhere(),
getPluralAttributeSource().getWhere()
)
);
elementBinding.setReferencedEntityName( referencedEntityBinding.getEntityName() );
elementBinding.setAssociatedClass( referencedEntityBinding );
elementBinding.setIgnoreNotFound( elementSource.isIgnoreNotFound() );
}
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceManyToMany ) {
final PluralAttributeElementSourceManyToMany elementSource =
(PluralAttributeElementSourceManyToMany) getPluralAttributeSource().getElementSource();
else if ( getPluralAttributeSource().getElementSource() instanceof PluralAttributeElementSourceManyToMany elementSource ) {
final ManyToOne elementBinding = new ManyToOne(
getMappingDocument(),
getCollectionBinding().getCollectionTable()
@ -3466,24 +3419,12 @@ public class ModelBinder {
// This "where" clause comes from the collection mapping; e.g., <set name="..." ... where="..." .../>
getCollectionBinding().setWhere( getPluralAttributeSource().getWhere() );
if ( useEntityWhereClauseForCollections( metadataBuildingContext ) ) {
// 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(
getNonEmptyOrConjunctionIfBothNonEmpty(
referencedEntityBinding.getWhere(),
elementSource.getWhere()
)
);
}
else {
// ignore entity's where clause
getCollectionBinding().setManyToManyWhere( elementSource.getWhere() );
}
getCollectionBinding().setManyToManyWhere(
getNonEmptyOrConjunctionIfBothNonEmpty(
referencedEntityBinding.getWhere(),
elementSource.getWhere()
)
);
getCollectionBinding().setManyToManyOrdering( elementSource.getOrder() );

View File

@ -7,7 +7,6 @@
package org.hibernate.cfg;
import org.hibernate.Incubating;
import org.hibernate.Remove;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.ListIndexBase;
import org.hibernate.annotations.Nationalized;
@ -453,28 +452,6 @@ public interface MappingSettings {
*/
String DEFAULT_LIST_SEMANTICS = "hibernate.mapping.default_list_semantics";
/**
* The {@link org.hibernate.annotations.SQLRestriction @SQLRestriction} annotation specifies a
* restriction on the table rows which are visible as entity class instances or
* collection elements.
* <p>
* This setting controls whether the restriction applied to an entity should be
* applied to association fetches (for one-to-one, many-to-one, one-to-many, and
* many-to-many associations) which target the entity.
*
* @apiNote The setting is very misnamed - it applies across all entity associations,
* not only to collections.
*
* @implSpec Enabled ({@code true}) by default, meaning the restriction is applied.
* When this setting is explicitly disabled ({@code false}), the restriction
* is not applied.
*
* @deprecated Originally added as a backwards compatibility flag
*/
@Remove
@Deprecated( forRemoval = true, since = "6.2" )
String USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = "hibernate.use_entity_where_clause_for_collections";
/**
* Whether XML should be validated against their schema as Hibernate reads them.
*

View File

@ -9,6 +9,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
@ -25,17 +26,11 @@ import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
@Jpa(
annotatedClasses = {
EagerManyToOneTest.Child.class,
EagerManyToOneTest.Parent.class
},
properties = @Setting( name = AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, value = "false")
)
@TestForIssue(jiraKey = "HHH-15902")
@SuppressWarnings("JUnitMalformedDeclaration")
@Jpa( annotatedClasses = { EagerManyToOneTest.Child.class, EagerManyToOneTest.Parent.class } )
@JiraKey("HHH-15902")
public class EagerManyToOneTest {
@BeforeEach
@ -62,12 +57,12 @@ public class EagerManyToOneTest {
@Test
public void testFindParent(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Parent parent = entityManager.find( Parent.class, 1 );
assertThat( parent ).isNotNull();
assertThat( parent.children.size() ).isEqualTo( 1 );
// the Child filter should apply
assertThat( parent.children ).isEmpty();
}
);
scope.inTransaction(

View File

@ -1,213 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.where.annotations;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
*
* @author Gail Badner
*/
public class EagerToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category flowers = new Category();
flowers.id = 1;
flowers.name = "flowers";
flowers.description = "FLOWERS";
product.categoriesOneToMany.add( flowers );
product.categoriesWithDescOneToMany.add( flowers );
product.categoriesManyToMany.add( flowers );
product.categoriesWithDescManyToMany.add( flowers );
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
Category vegetables = new Category();
vegetables.id = 2;
vegetables.name = "vegetables";
vegetables.description = "VEGETABLES";
product.categoriesOneToMany.add( vegetables );
product.categoriesWithDescOneToMany.add( vegetables );
product.categoriesManyToMany.add( vegetables );
product.categoriesWithDescManyToMany.add( vegetables );
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
Category dogs = new Category();
dogs.id = 3;
dogs.name = "dogs";
dogs.description = null;
product.categoriesOneToMany.add( dogs );
product.categoriesWithDescOneToMany.add( dogs );
product.categoriesManyToMany.add( dogs );
product.categoriesWithDescManyToMany.add( dogs );
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
Category building = new Category();
building.id = 4;
building.name = "building";
building.description = "BUILDING";
product.categoriesOneToMany.add( building );
product.categoriesWithDescOneToMany.add( building );
product.categoriesManyToMany.add( building );
product.categoriesWithDescManyToMany.add( building );
product.categoriesWithDescIdLt4ManyToMany.add( building );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( flowers );
session.persist( vegetables );
session.persist( dogs );
session.persist( building );
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertEquals( 4, p.categoriesOneToMany.size() );
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.categoriesManyToMany.size() );
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.id );
assertNotNull( c );
c.inactive = 1;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertEquals( 4, p.categoriesOneToMany.size() );
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.categoriesManyToMany.size() );
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
}
);
}
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
for ( Category category : categories ) {
expectedIdSet.remove( category.id );
}
assertTrue( expectedIdSet.isEmpty() );
}
@Entity(name = "Product")
public static class Product {
@Id
@GeneratedValue
private int id;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn
private Set<Category> categoriesOneToMany = new HashSet<>();
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn
@SQLRestriction( "description is not null" )
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "categoriesManyToMany")
private Set<Category> categoriesManyToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "categoriesWithDescManyToMany", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
@SQLRestriction( "description is not null" )
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
@SQLRestriction( "description is not null" )
@SQLJoinTableRestriction( "categoryId < 4")
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
}
@Entity(name = "Category")
@Table(name = "CATEGORY")
@SQLRestriction( "inactive = 0")
public static class Category {
@Id
private int id;
private String name;
private String description;
private int inactive;
}
}

View File

@ -8,8 +8,15 @@ package org.hibernate.orm.test.where.annotations;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
@ -20,14 +27,6 @@ import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -35,24 +34,17 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
*
* @author Gail Badner
*/
public class EagerToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
@JiraKey("HHH-13011")
public void testAssociatedWhereClause() {
Product product = new Product();

View File

@ -1,213 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.where.annotations;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
*
* @author Gail Badner
*/
public class LazyToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category flowers = new Category();
flowers.id = 1;
flowers.name = "flowers";
flowers.description = "FLOWERS";
product.categoriesOneToMany.add( flowers );
product.categoriesWithDescOneToMany.add( flowers );
product.categoriesManyToMany.add( flowers );
product.categoriesWithDescManyToMany.add( flowers );
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
Category vegetables = new Category();
vegetables.id = 2;
vegetables.name = "vegetables";
vegetables.description = "VEGETABLES";
product.categoriesOneToMany.add( vegetables );
product.categoriesWithDescOneToMany.add( vegetables );
product.categoriesManyToMany.add( vegetables );
product.categoriesWithDescManyToMany.add( vegetables );
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
Category dogs = new Category();
dogs.id = 3;
dogs.name = "dogs";
dogs.description = null;
product.categoriesOneToMany.add( dogs );
product.categoriesWithDescOneToMany.add( dogs );
product.categoriesManyToMany.add( dogs );
product.categoriesWithDescManyToMany.add( dogs );
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
Category building = new Category();
building.id = 4;
building.name = "building";
building.description = "BUILDING";
product.categoriesOneToMany.add( building );
product.categoriesWithDescOneToMany.add( building );
product.categoriesManyToMany.add( building );
product.categoriesWithDescManyToMany.add( building );
product.categoriesWithDescIdLt4ManyToMany.add( building );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( flowers );
session.persist( vegetables );
session.persist( dogs );
session.persist( building );
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertEquals( 4, p.categoriesOneToMany.size() );
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.categoriesManyToMany.size() );
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.id );
assertNotNull( c );
c.inactive = 1;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertEquals( 4, p.categoriesOneToMany.size() );
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.categoriesManyToMany.size() );
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
}
);
}
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
for ( Category category : categories ) {
expectedIdSet.remove( category.id );
}
assertTrue( expectedIdSet.isEmpty() );
}
@Entity(name = "Product")
public static class Product {
@Id
@GeneratedValue
private int id;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn
private Set<Category> categoriesOneToMany = new HashSet<>();
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn
@SQLRestriction( "description is not null" )
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "categoriesManyToMany")
private Set<Category> categoriesManyToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "categoriesWithDescManyToMany", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
@SQLRestriction( "description is not null" )
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
@SQLRestriction( "description is not null" )
@SQLJoinTableRestriction( "categoryId < 4")
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
}
@Entity(name = "Category")
@Table(name = "CATEGORY")
@SQLRestriction("inactive = 0")
public static class Category {
@Id
private int id;
private String name;
private String description;
private int inactive;
}
}

View File

@ -8,8 +8,15 @@ package org.hibernate.orm.test.where.annotations;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
@ -20,14 +27,6 @@ import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -35,24 +34,17 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
*
* @author Gail Badner
*/
public class LazyToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
@JiraKey( "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();

View File

@ -1,162 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.where.hbm;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
*
* @author Gail Badner
*/
public class EagerToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected String getBaseForMappings() {
return "org/hibernate/orm/test/";
}
@Override
protected String[] getMappings() {
return new String[] { "where/hbm/EagerToManyWhere.hbm.xml" };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category flowers = new Category();
flowers.setId( 1 );
flowers.setName( "flowers" );
flowers.setDescription( "FLOWERS" );
product.getCategoriesOneToMany().add( flowers );
product.getCategoriesWithDescOneToMany().add( flowers );
product.getCategoriesManyToMany().add( flowers );
product.getCategoriesWithDescManyToMany().add( flowers );
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
Category vegetables = new Category();
vegetables.setId( 2 );
vegetables.setName( "vegetables" );
vegetables.setDescription( "VEGETABLES" );
product.getCategoriesOneToMany().add( vegetables );
product.getCategoriesWithDescOneToMany().add( vegetables );
product.getCategoriesManyToMany().add( vegetables );
product.getCategoriesWithDescManyToMany().add( vegetables );
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
Category dogs = new Category();
dogs.setId( 3 );
dogs.setName( "dogs" );
dogs.setDescription( null );
product.getCategoriesOneToMany().add( dogs );
product.getCategoriesWithDescOneToMany().add( dogs );
product.getCategoriesManyToMany().add( dogs );
product.getCategoriesWithDescManyToMany().add( dogs );
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
Category building = new Category();
building.setId( 4 );
building.setName( "building" );
building.setDescription( "BUILDING" );
product.getCategoriesOneToMany().add( building );
product.getCategoriesWithDescOneToMany().add( building );
product.getCategoriesManyToMany().add( building );
product.getCategoriesWithDescManyToMany().add( building );
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( flowers );
session.persist( vegetables );
session.persist( dogs );
session.persist( building );
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.getId() );
assertNotNull( p );
assertEquals( 4, p.getCategoriesOneToMany().size() );
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.getCategoriesManyToMany().size() );
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.getId() );
assertNotNull( c );
c.setInactive( 1 );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.getId() );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.getId() );
assertNotNull( p );
assertEquals( 4, p.getCategoriesOneToMany().size() );
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.getCategoriesManyToMany().size() );
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
}
);
}
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
for ( Category category : categories ) {
expectedIdSet.remove( category.getId() );
}
assertTrue( expectedIdSet.isEmpty() );
}
}

View File

@ -15,6 +15,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
@ -24,8 +25,6 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
*
* @author Gail Badner
*/
public class EagerToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@ -40,13 +39,8 @@ public class EagerToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunction
return new String[] { "where/hbm/EagerToManyWhere.hbm.xml" };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
@JiraKey( "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();

View File

@ -1,162 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.where.hbm;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
*
* @author Gail Badner
*/
public class LazyToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected String getBaseForMappings() {
return "org/hibernate/orm/test/";
}
@Override
protected String[] getMappings() {
return new String[] { "where/hbm/LazyToManyWhere.hbm.xml" };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category flowers = new Category();
flowers.setId( 1 );
flowers.setName( "flowers" );
flowers.setDescription( "FLOWERS" );
product.getCategoriesOneToMany().add( flowers );
product.getCategoriesWithDescOneToMany().add( flowers );
product.getCategoriesManyToMany().add( flowers );
product.getCategoriesWithDescManyToMany().add( flowers );
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
Category vegetables = new Category();
vegetables.setId( 2 );
vegetables.setName( "vegetables" );
vegetables.setDescription( "VEGETABLES" );
product.getCategoriesOneToMany().add( vegetables );
product.getCategoriesWithDescOneToMany().add( vegetables );
product.getCategoriesManyToMany().add( vegetables );
product.getCategoriesWithDescManyToMany().add( vegetables );
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
Category dogs = new Category();
dogs.setId( 3 );
dogs.setName( "dogs" );
dogs.setDescription( null );
product.getCategoriesOneToMany().add( dogs );
product.getCategoriesWithDescOneToMany().add( dogs );
product.getCategoriesManyToMany().add( dogs );
product.getCategoriesWithDescManyToMany().add( dogs );
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
Category building = new Category();
building.setId( 4 );
building.setName( "building" );
building.setDescription( "BUILDING" );
product.getCategoriesOneToMany().add( building );
product.getCategoriesWithDescOneToMany().add( building );
product.getCategoriesManyToMany().add( building );
product.getCategoriesWithDescManyToMany().add( building );
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( flowers );
session.persist( vegetables );
session.persist( dogs );
session.persist( building );
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.getId() );
assertNotNull( p );
assertEquals( 4, p.getCategoriesOneToMany().size() );
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.getCategoriesManyToMany().size() );
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.getId() );
assertNotNull( c );
c.setInactive( 1 );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, flowers.getId() );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.getId() );
assertNotNull( p );
assertEquals( 4, p.getCategoriesOneToMany().size() );
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 4, p.getCategoriesManyToMany().size() );
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
}
);
}
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
for ( Category category : categories ) {
expectedIdSet.remove( category.getId() );
}
assertTrue( expectedIdSet.isEmpty() );
}
}

View File

@ -8,13 +8,10 @@ package org.hibernate.orm.test.where.hbm;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
@ -24,8 +21,6 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
*
* @author Gail Badner
*/
public class LazyToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@ -40,13 +35,8 @@ public class LazyToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctiona
return new String[] { "where/hbm/LazyToManyWhere.hbm.xml" };
}
@Override
protected void addSettings(Map<String,Object> settings) {
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
}
@Test
@TestForIssue( jiraKey = "HHH-13011" )
@JiraKey( "HHH-13011" )
public void testAssociatedWhereClause() {
Product product = new Product();