Skip to content

Commit 34110f5

Browse files
committed
HHH-19036 Process all filters before going through classes
1 parent 84e5d19 commit 34110f5

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotationBinder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ private static void bindNamedStoredProcedureQuery(
371371
}
372372
}
373373

374+
public static void preBindClass(
375+
XClass annotatedClass,
376+
MetadataBuildingContext context) throws MappingException {
377+
bindFilterDefs( annotatedClass, context );
378+
}
379+
374380
/**
375381
* Bind an annotated class. A subclass must be bound <em>after</em> its superclass.
376382
*
@@ -388,7 +394,6 @@ public static void bindClass(
388394

389395
bindQueries( annotatedClass, context );
390396
handleImport( annotatedClass, context );
391-
bindFilterDefs( annotatedClass, context );
392397
bindTypeDescriptorRegistrations( annotatedClass, context );
393398
bindEmbeddableInstantiatorRegistrations( annotatedClass, context );
394399
bindUserTypeRegistrations( annotatedClass, context );

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
package org.hibernate.boot.model.source.internal.annotations;
88

99
import java.util.ArrayList;
10+
import java.util.HashSet;
1011
import java.util.LinkedHashSet;
1112
import java.util.List;
1213
import java.util.Map;
1314
import java.util.Set;
15+
import java.util.TreeSet;
1416

1517
import jakarta.persistence.AttributeConverter;
1618
import jakarta.persistence.Converter;
@@ -37,6 +39,8 @@
3739
import org.jboss.jandex.IndexView;
3840
import org.jboss.logging.Logger;
3941

42+
import static org.hibernate.boot.model.internal.AnnotationBinder.preBindClass;
43+
4044
/**
4145
* @author Steve Ebersole
4246
*/
@@ -248,6 +252,13 @@ public void processEntityHierarchies(Set<String> processedEntityNames) {
248252
orderedClasses,
249253
rootMetadataBuildingContext
250254
);
255+
// we want to go through all classes and collect the filter definitions first,
256+
// so that when we bind the classes we have the complete list of filters to search from:
257+
for ( XClass clazz : orderedClasses ) {
258+
if ( !processedEntityNames.contains( clazz.getName() ) ) {
259+
preBindClass( clazz, rootMetadataBuildingContext );
260+
}
261+
}
251262

252263
for ( XClass clazz : orderedClasses ) {
253264
if ( processedEntityNames.contains( clazz.getName() ) ) {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.filter;
8+
9+
import jakarta.persistence.Column;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.GenerationType;
13+
import jakarta.persistence.Id;
14+
import org.hibernate.annotations.Filter;
15+
import org.hibernate.annotations.FilterDef;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.JiraKey;
18+
import org.junit.jupiter.api.Test;
19+
20+
@DomainModel(
21+
annotatedClasses = {
22+
FilterDefinitionOrderTest.AMyEntity.class,
23+
FilterDefinitionOrderTest.XEntity.class
24+
}
25+
)
26+
@JiraKey("HHH-19036")
27+
public class FilterDefinitionOrderTest extends AbstractStatefulStatelessFilterTest {
28+
29+
@Test
30+
public void smokeTest() {
31+
// nothing really to test here,
32+
// if the test starts, then everything is great!
33+
}
34+
35+
@Entity
36+
@Filter(name = "x_filter")
37+
public static class AMyEntity {
38+
@Id
39+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
40+
@Column(nullable = false)
41+
private Long id;
42+
43+
public String field;
44+
45+
public Long getId() {
46+
return id;
47+
}
48+
49+
public void setId(Long id) {
50+
this.id = id;
51+
}
52+
53+
public String getField() {
54+
return field;
55+
}
56+
57+
public void setField(String field) {
58+
this.field = field;
59+
}
60+
}
61+
62+
@Entity
63+
@FilterDef(name = "x_filter", defaultCondition = "field = 'Hello'")
64+
public static class XEntity {
65+
66+
@Id
67+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
68+
@Column(nullable = false)
69+
private Long id;
70+
71+
public String field;
72+
73+
public Long getId() {
74+
return id;
75+
}
76+
77+
public void setId(Long id) {
78+
this.id = id;
79+
}
80+
81+
public String getField() {
82+
return field;
83+
}
84+
85+
public void setField(String field) {
86+
this.field = field;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)