5
5
package org .hibernate .orm .test .jpa .ql ;
6
6
7
7
import org .hibernate .dialect .PostgreSQLDialect ;
8
-
9
8
import org .hibernate .orm .test .jpa .model .MapContent ;
10
9
import org .hibernate .orm .test .jpa .model .MapOwner ;
11
10
import org .hibernate .orm .test .jpa .model .Relationship ;
12
- import org .hibernate .testing .orm .junit .JiraKey ;
13
11
import org .hibernate .testing .jdbc .SQLStatementInspector ;
14
12
import org .hibernate .testing .orm .junit .DomainModel ;
13
+ import org .hibernate .testing .orm .junit .JiraKey ;
15
14
import org .hibernate .testing .orm .junit .RequiresDialect ;
16
15
import org .hibernate .testing .orm .junit .SessionFactory ;
17
16
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18
17
import org .junit .jupiter .api .Test ;
19
18
19
+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
20
20
@ JiraKey (value = "HHH-14279" )
21
- @ DomainModel (
22
- annotatedClasses = {
23
- MapOwner .class , MapContent .class , Relationship .class
24
- })
21
+ @ DomainModel ( annotatedClasses = { MapOwner .class , MapContent .class , Relationship .class } )
25
22
@ SessionFactory (useCollectingStatementInspector = true )
26
23
public class MapIssueTest {
27
24
28
25
@ Test
29
26
@ RequiresDialect (value = PostgreSQLDialect .class , comment = "Requires support for using a correlated column in a join condition which H2 apparently does not support. For simplicity just run this on PostgreSQL" )
30
27
public void testWhereSubqueryMapKeyIsEntityWhereWithKey (SessionFactoryScope scope ) {
31
- scope .inTransaction (
32
- s -> {
33
- s .createQuery ( "select r from Relationship r where exists (select 1 from MapOwner as o left join o.contents c with key(c) = r)" ).list ();
28
+ scope .inTransaction ( ( session ) -> {
29
+ //noinspection deprecation
30
+ session .createQuery ( "select r from Relationship r where exists (select 1 from MapOwner as o left join o.contents c with key(c) = r)" ).list ();
34
31
}
35
32
);
36
33
}
@@ -39,112 +36,104 @@ public void testWhereSubqueryMapKeyIsEntityWhereWithKey(SessionFactoryScope scop
39
36
public void testOnlyCollectionTableJoined (SessionFactoryScope scope ) {
40
37
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
41
38
statementInspector .clear ();
42
- scope .inTransaction (
43
- s -> {
44
- s .createQuery ( "select 1 from MapOwner as o left join o.contents c where c.id is not null" ).list ();
45
- statementInspector .assertExecutedCount ( 1 );
46
- // Assert only the collection table is joined
47
- statementInspector .assertNumberOfJoins ( 0 , 1 );
48
- }
49
- );
39
+ scope .inTransaction ( (session ) -> {
40
+ //noinspection deprecation
41
+ session .createQuery ( "select 1 from MapOwner as o left join o.contents c where c.id is not null" ).list ();
42
+ statementInspector .assertExecutedCount ( 1 );
43
+ // Assert only the collection table is joined
44
+ statementInspector .assertNumberOfJoins ( 0 , 1 );
45
+ } );
50
46
}
51
47
52
48
@ Test
53
49
public void testMapKeyJoinIsNotOmitted (SessionFactoryScope scope ) {
54
50
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
55
51
statementInspector .clear ();
56
- scope .inTransaction (
57
- s -> {
58
- s .createQuery ( "select c from MapOwner as o join o.contents c join c.relationship r where r.id is not null" ).list ();
59
- statementInspector .assertExecutedCount ( 1 );
60
- // Assert 3 joins, collection table, collection element and collection key (relationship)
61
- statementInspector .assertNumberOfJoins ( 0 , 3 );
62
- }
63
- );
52
+ scope .inTransaction ( (session ) -> {
53
+ //noinspection deprecation
54
+ session .createQuery ( "select c from MapOwner as o join o.contents c join c.relationship r where r.id is not null" ).list ();
55
+ statementInspector .assertExecutedCount ( 1 );
56
+ // Assert 3 joins, collection table, collection element and collection key (relationship)
57
+ statementInspector .assertNumberOfJoins ( 0 , 3 );
58
+ } );
64
59
}
65
60
66
61
@ Test
67
62
public void testMapKeyJoinIsOmitted2 (SessionFactoryScope scope ) {
68
63
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
69
64
statementInspector .clear ();
70
- scope .inTransaction (
71
- s -> {
72
- s .createQuery ( "select c from MapOwner as o join o.contents c where c.relationship.id is not null" ).list ();
73
- statementInspector .assertExecutedCount ( 1 );
74
- // Assert 2 joins, collection table and collection element. No need to join the relationship because it is not nullable
75
- statementInspector .assertNumberOfJoins ( 0 , 2 );
76
- }
77
- );
65
+ scope .inTransaction ( (session ) -> {
66
+ //noinspection deprecation
67
+ session .createQuery ( "select c from MapOwner as o join o.contents c where c.relationship.id is not null" ).list ();
68
+ statementInspector .assertExecutedCount ( 1 );
69
+ // Assert 2 joins, collection table and collection element. No need to join the relationship because it is not nullable
70
+ statementInspector .assertNumberOfJoins ( 0 , 2 );
71
+ } );
78
72
}
79
73
80
74
@ Test
81
75
public void testMapKeyDeReferenceDoesNotCauseJoin (SessionFactoryScope scope ) {
82
76
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
83
77
statementInspector .clear ();
84
- scope .inTransaction (
85
- s -> {
86
- s .createQuery ( "select c from MapOwner as o left join o.contents c where key(c).id is not null" ).list ();
87
- statementInspector .assertExecutedCount ( 1 );
88
- // Assert 2 joins, collection table and collection element
89
- statementInspector .assertNumberOfJoins ( 0 , 2 );
90
- }
91
- );
78
+ scope .inTransaction ( (session ) -> {
79
+ //noinspection deprecation
80
+ session .createQuery ( "select c from MapOwner as o left join o.contents c where key(c).id is not null" ).list ();
81
+ statementInspector .assertExecutedCount ( 1 );
82
+ // Assert 2 joins, collection table and collection element
83
+ statementInspector .assertNumberOfJoins ( 0 , 2 );
84
+ } );
92
85
}
93
86
94
87
@ Test
95
88
public void testMapKeyJoinIsReused (SessionFactoryScope scope ) {
96
89
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
97
90
statementInspector .clear ();
98
- scope .inTransaction (
99
- s -> {
100
- s .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r where r.name is not null" ).list ();
101
- statementInspector .assertExecutedCount ( 1 );
102
- // Assert 3 joins, collection table, collection element and relationship
103
- statementInspector .assertNumberOfJoins ( 0 , 3 );
104
- }
105
- );
91
+ scope .inTransaction ( (s ) -> {
92
+ //noinspection deprecation
93
+ s .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r where r.name is not null" ).list ();
94
+ statementInspector .assertExecutedCount ( 1 );
95
+ // Assert 3 joins, collection table, collection element and relationship
96
+ statementInspector .assertNumberOfJoins ( 0 , 3 );
97
+ } );
106
98
}
107
99
108
100
@ Test
109
101
public void testMapKeyJoinIsReusedForFurtherJoin (SessionFactoryScope scope ) {
110
102
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
111
103
statementInspector .clear ();
112
- scope .inTransaction (
113
- s -> {
114
- s .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r join r.self s where s.name is not null" ).list ();
115
- statementInspector .assertExecutedCount ( 1 );
116
- // Assert 3 joins, collection table, collection element, relationship and self
117
- statementInspector .assertNumberOfJoins ( 0 , 4 );
118
- }
119
- );
104
+ scope .inTransaction ( (session ) -> {
105
+ //noinspection deprecation
106
+ session .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r join r.self s where s.name is not null" ).list ();
107
+ statementInspector .assertExecutedCount ( 1 );
108
+ // Assert 3 joins, collection table, collection element, relationship and self
109
+ statementInspector .assertNumberOfJoins ( 0 , 4 );
110
+ } );
120
111
}
121
112
122
113
@ Test
123
114
public void testMapKeyJoinIsReusedForFurtherJoinAndElementJoinIsProperlyOrdered (SessionFactoryScope scope ) {
124
115
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
125
116
statementInspector .clear ();
126
- scope .inTransaction (
127
- s -> {
128
- s .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r join r.self s join c.relationship2 where s.name is not null" ).list ();
129
- statementInspector .assertExecutedCount ( 1 );
130
- // Assert 3 joins, collection table, collection element, relationship, relationship2 and self
131
- statementInspector .assertNumberOfJoins ( 0 , 5 );
132
- }
133
- );
117
+ scope .inTransaction ( (session ) -> {
118
+ //noinspection deprecation
119
+ session .createQuery ( "select key(c), c from MapOwner as o left join o.contents c join c.relationship r join r.self s join c.relationship2 where s.name is not null" ).list ();
120
+ statementInspector .assertExecutedCount ( 1 );
121
+ // Assert 3 joins, collection table, collection element, relationship, relationship2 and self
122
+ statementInspector .assertNumberOfJoins ( 0 , 5 );
123
+ } );
134
124
}
135
125
136
126
@ Test
137
127
@ JiraKey ( value = "HHH-15357" )
138
128
public void testSelectMapKeyFk (SessionFactoryScope scope ) {
139
129
SQLStatementInspector statementInspector = scope .getCollectingStatementInspector ();
140
130
statementInspector .clear ();
141
- scope .inTransaction (
142
- s -> {
143
- s .createQuery ( "select key(c).id from MapOwner as o left join o.contents c" ).list ();
144
- statementInspector .assertExecutedCount ( 1 );
145
- // Assert that only the collection table and element table are joined
146
- statementInspector .assertNumberOfJoins ( 0 , 2 );
147
- }
148
- );
131
+ scope .inTransaction ( (session ) -> {
132
+ //noinspection deprecation
133
+ session .createQuery ( "select key(c).id from MapOwner as o left join o.contents c" ).list ();
134
+ statementInspector .assertExecutedCount ( 1 );
135
+ // Assert that only the collection table and element table are joined
136
+ statementInspector .assertNumberOfJoins ( 0 , 2 );
137
+ } );
149
138
}
150
139
}
0 commit comments