15
15
*/
16
16
package org .springframework .data .jpa .repository ;
17
17
18
- import static org .assertj .core .api .Assertions .* ;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
19
20
20
import jakarta .persistence .EntityManager ;
21
21
import jakarta .persistence .criteria .CriteriaBuilder ;
22
22
import jakarta .persistence .criteria .CriteriaQuery ;
23
23
import jakarta .persistence .criteria .Predicate ;
24
24
import jakarta .persistence .criteria .Root ;
25
25
26
+ import java .util .List ;
27
+
26
28
import org .junit .jupiter .api .AfterEach ;
27
29
import org .junit .jupiter .api .BeforeEach ;
28
30
import org .junit .jupiter .api .Test ;
29
31
import org .junit .jupiter .api .extension .ExtendWith ;
30
32
import org .springframework .beans .factory .annotation .Autowired ;
31
33
import org .springframework .data .domain .Example ;
34
+ import org .springframework .data .domain .ExampleMatcher ;
32
35
import org .springframework .data .jpa .convert .QueryByExamplePredicateBuilder ;
33
36
import org .springframework .data .jpa .domain .sample .Role ;
37
+ import org .springframework .data .jpa .domain .sample .User ;
34
38
import org .springframework .data .jpa .repository .sample .RoleRepository ;
39
+ import org .springframework .data .jpa .repository .sample .UserRepository ;
35
40
import org .springframework .test .context .ContextConfiguration ;
36
41
import org .springframework .test .context .junit .jupiter .SpringExtension ;
37
42
import org .springframework .transaction .annotation .Transactional ;
38
43
39
44
/**
40
45
* @author Greg Turnquist
46
+ * @author Christoph Strobl
41
47
* @since 3.0
42
48
*/
43
49
@ ExtendWith (SpringExtension .class )
44
50
@ ContextConfiguration ({ "classpath:hibernate.xml" , "classpath:config/namespace-application-context.xml" })
45
51
@ Transactional
46
52
class QueryByExampleIntegrationTests {
47
53
48
- @ Autowired RoleRepository repository ;
54
+ @ Autowired RoleRepository roleRepository ;
55
+ @ Autowired UserRepository userRepository ;
49
56
@ Autowired EntityManager em ;
50
57
51
58
private Role drummer ;
@@ -55,14 +62,14 @@ class QueryByExampleIntegrationTests {
55
62
@ BeforeEach
56
63
void setUp () {
57
64
58
- drummer = repository .save (new Role ("drummer" ));
59
- guitarist = repository .save (new Role ("guitarist" ));
60
- singer = repository .save (new Role ("singer" ));
65
+ drummer = roleRepository .save (new Role ("drummer" ));
66
+ guitarist = roleRepository .save (new Role ("guitarist" ));
67
+ singer = roleRepository .save (new Role ("singer" ));
61
68
}
62
69
63
70
@ AfterEach
64
71
void clearUp () {
65
- repository .deleteAll ();
72
+ roleRepository .deleteAll ();
66
73
}
67
74
68
75
@ Test // GH-2283
@@ -81,6 +88,39 @@ void queryByExampleWithNoPredicatesShouldHaveNoWhereClause() {
81
88
82
89
// then
83
90
assertThat (predicate ).isNull ();
84
- assertThat (repository .findAll (example )).containsExactlyInAnyOrder (drummer , guitarist , singer );
91
+ assertThat (roleRepository .findAll (example )).containsExactlyInAnyOrder (drummer , guitarist , singer );
92
+ }
93
+
94
+ @ Test // GH-3763
95
+ void usesAnyMatchOnJoins () {
96
+
97
+ User manager =
new User (
"mighty" ,
"super user" ,
"[email protected] " );
98
+
99
+ userRepository .save (manager );
100
+
101
+ User dave = new User ();
102
+ dave .setFirstname ("dave" );
103
+ dave .setLastname ("matthews" );
104
+ dave .
setEmailAddress (
"[email protected] " );
105
+ dave .addRole (singer );
106
+
107
+ User carter = new User ();
108
+ carter .setFirstname ("carter" );
109
+ carter .setLastname ("beaufort" );
110
+ carter .
setEmailAddress (
"[email protected] " );
111
+ carter .addRole (drummer );
112
+ carter .addRole (singer );
113
+ carter .setManager (manager );
114
+
115
+ userRepository .saveAllAndFlush (List .of (dave , carter ));
116
+
117
+ User probe = new User ();
118
+ probe .setLastname (dave .getLastname ());
119
+ probe .setManager (manager );
120
+
121
+ Example <User > example = Example .of (probe ,
122
+ ExampleMatcher .matchingAny ().withIgnorePaths ("id" , "createdAt" , "age" , "active" , "emailAddress" ,
123
+ "secondaryEmailAddress" , "colleagues" , "address" , "binaryData" , "attributes" , "dateOfBirth" ));
124
+ assertThat (userRepository .findAll (example )).containsExactly (dave , carter );
85
125
}
86
126
}
0 commit comments