Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.type.descriptor.java.BasicJavaType;
Expand All @@ -69,7 +68,6 @@

import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
import static org.hibernate.boot.model.internal.AnnotatedClassType.ENTITY;
import static org.hibernate.boot.model.internal.FilterDefBinder.bindFilterDefs;
import static org.hibernate.boot.model.internal.GeneratorBinder.buildGenerators;
import static org.hibernate.boot.model.internal.GeneratorBinder.buildIdGenerator;
import static org.hibernate.boot.model.internal.InheritanceState.getInheritanceStateOfSuperEntity;
Expand Down Expand Up @@ -226,7 +224,7 @@ public static void bindPackage(ClassLoaderService cls, String packageName, Metad

bindGenericGenerators( annotatedPackage, context );
bindQueries( annotatedPackage, context );
bindFilterDefs( annotatedPackage, context );
FilterDefBinder.bindFilterDefs( annotatedPackage, context );
}

private static void handleIdGenerators(XPackage annotatedPackage, MetadataBuildingContext context) {
Expand Down Expand Up @@ -371,6 +369,12 @@ private static void bindNamedStoredProcedureQuery(
}
}

public static void bindFilterDefs(
XClass annotatedClass,
MetadataBuildingContext context) throws MappingException {
FilterDefBinder.bindFilterDefs( annotatedClass, context );
}

/**
* Bind an annotated class. A subclass must be bound <em>after</em> its superclass.
*
Expand All @@ -388,7 +392,6 @@ public static void bindClass(

bindQueries( annotatedClass, context );
handleImport( annotatedClass, context );
bindFilterDefs( annotatedClass, context );
bindTypeDescriptorRegistrations( annotatedClass, context );
bindEmbeddableInstantiatorRegistrations( annotatedClass, context );
bindUserTypeRegistrations( annotatedClass, context );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;

import static org.hibernate.boot.model.internal.AnnotationBinder.bindFilterDefs;

/**
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -248,6 +250,13 @@ public void processEntityHierarchies(Set<String> processedEntityNames) {
orderedClasses,
rootMetadataBuildingContext
);
// we want to go through all classes and collect the filter definitions first,
// so that when we bind the classes we have the complete list of filters to search from:
for ( XClass clazz : orderedClasses ) {
if ( !processedEntityNames.contains( clazz.getName() ) ) {
bindFilterDefs( clazz, rootMetadataBuildingContext );
}
}

for ( XClass clazz : orderedClasses ) {
if ( processedEntityNames.contains( clazz.getName() ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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.filter;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import org.hibernate.SharedSessionContract;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static org.assertj.core.api.Assertions.assertThat;

@DomainModel(
annotatedClasses = {
FilterDefinitionOrderTest.AMyEntity.class,
FilterDefinitionOrderTest.XEntity.class
}
)
@JiraKey("HHH-19036")
// Real test is if it actually starts, as that's where the filter binding happens,
// but let's also check that it was actually processed as well:
public class FilterDefinitionOrderTest extends AbstractStatefulStatelessFilterTest {

@BeforeEach
void setUp() {
scope.inTransaction(session -> {
AMyEntity entity1 = new AMyEntity();
entity1.setField("test");
AMyEntity entity2 = new AMyEntity();
entity2.setField("Hello");
session.persist(entity1);
session.persist(entity2);
});
}

@AfterEach
void tearDown() {
scope.inTransaction(session -> {
session.createMutationQuery("delete from AMyEntity").executeUpdate();
});
}

@ParameterizedTest
@MethodSource("transactionKind")
void smokeTest(BiConsumer<SessionFactoryScope, Consumer<? extends SharedSessionContract>> inTransaction) {
inTransaction.accept(scope, session -> {
session.enableFilter("x_filter");
List<AMyEntity> entities = session.createQuery("FROM AMyEntity", AMyEntity.class).getResultList();
assertThat(entities).hasSize(1)
.allSatisfy(entity -> assertThat(entity.getField()).isEqualTo("Hello"));

session.disableFilter("x_filter");
entities = session.createQuery("FROM AMyEntity", AMyEntity.class).getResultList();
assertThat(entities).hasSize(2);
});
}

@Entity(name = "AMyEntity")
@Filter(name = "x_filter")
public static class AMyEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(nullable = false)
private Long id;

public String field;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}
}

@Entity(name = "XEntity")
@FilterDef(name = "x_filter", defaultCondition = "field = 'Hello'")
public static class XEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(nullable = false)
private Long id;

public String field;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}
}
}
Loading