10
10
11
11
import org .hibernate .testing .orm .junit .DialectFeatureChecks ;
12
12
import org .hibernate .testing .orm .junit .DomainModel ;
13
- import org .hibernate .testing .orm .junit .FailureExpected ;
13
+ import org .hibernate .testing .orm .junit .JiraKey ;
14
14
import org .hibernate .testing .orm .junit .RequiresDialectFeature ;
15
15
import org .hibernate .testing .orm .junit .SessionFactory ;
16
16
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
25
25
/**
26
26
* @author Jan Schatteman
27
27
*/
28
- @ DomainModel (
29
- annotatedClasses = { UnionOfPartitionResultsTest .Apple .class , UnionOfPartitionResultsTest .Pie .class }
28
+ @ DomainModel (
29
+ annotatedClasses = {UnionOfPartitionResultsTest .Apple .class , UnionOfPartitionResultsTest .Pie .class }
30
30
)
31
31
@ SessionFactory
32
+ @ JiraKey ( "HHH-18069" )
32
33
public class UnionOfPartitionResultsTest {
33
34
34
35
@ Test
35
- @ FailureExpected
36
36
@ RequiresDialectFeature (feature = DialectFeatureChecks .SupportsUnion .class )
37
+ public void testSubqueryWithUnion (SessionFactoryScope scope ) {
38
+ scope .inTransaction (
39
+ session -> {
40
+ String q = """
41
+ SELECT id
42
+ FROM
43
+ (
44
+ ( SELECT id id, bakedPie bakedPie
45
+ FROM Apple c
46
+ )
47
+ UNION ALL
48
+ ( SELECT id id, bakedPie bakedPie
49
+ FROM Apple a
50
+ )
51
+ )
52
+ """ ;
53
+
54
+ Query query = session .createQuery ( q );
55
+
56
+ query .list ();
57
+ }
58
+ );
59
+ }
60
+
61
+ @ Test
62
+ public void testSubquery (SessionFactoryScope scope ) {
63
+ scope .inTransaction (
64
+ session -> {
65
+ String q = """
66
+ SELECT id
67
+ FROM
68
+ (
69
+ SELECT id id, bakedPie bakedPie
70
+ FROM Apple c
71
+ )
72
+ """ ;
73
+
74
+ Query query = session .createQuery ( q );
75
+
76
+ query .list ();
77
+ }
78
+ );
79
+ }
80
+
81
+ @ Test
82
+ @ RequiresDialectFeature (feature = DialectFeatureChecks .SupportsUnion .class )
83
+ public void testUnionQuery (SessionFactoryScope scope ) {
84
+ scope .inTransaction (
85
+ session -> {
86
+ String q = """
87
+ ( SELECT id id, bakedPie bakedPie
88
+ FROM Apple c
89
+ )
90
+ UNION ALL
91
+ ( SELECT id id, bakedPie bakedPie
92
+ FROM Apple c
93
+ )
94
+ """ ;
95
+
96
+ Query query = session .createQuery ( q );
97
+
98
+ query .list ();
99
+ }
100
+ );
101
+ }
102
+
103
+
104
+ @ Test
105
+ @ RequiresDialectFeature (feature = DialectFeatureChecks .SupportsUnion .class )
106
+ @ RequiresDialectFeature (feature = DialectFeatureChecks .SupportPartitionBy .class )
37
107
public void testUnionOfPartitionResults (SessionFactoryScope scope ) {
38
108
scope .inTransaction (
39
109
session -> {
40
110
String q =
41
111
"SELECT new CurrentApple(id, bakedPie.id, dir) " +
42
112
"FROM (" +
43
- "(" +
44
- "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MAX(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, -1 dir " +
45
- "FROM Apple c " +
46
- "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn <= :now" +
47
- ") UNION ALL (" +
48
- "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MIN(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, 1 dir " +
49
- "FROM Apple c " +
50
- "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn > :now" +
51
- ")" +
113
+ "(" +
114
+ "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MAX(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, -1 dir " +
115
+ "FROM Apple c " +
116
+ "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn <= :now" +
117
+ ") UNION ALL (" +
118
+ "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MIN(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, 1 dir " +
119
+ "FROM Apple c " +
120
+ "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn > :now" +
121
+ ")" +
52
122
") " +
53
123
"WHERE bakedOn = mbo ORDER BY dir" ;
54
124
55
125
Query <CurrentApple > query = session .createQuery ( q , CurrentApple .class );
56
126
query .setParameter ( "now" , LocalDate .now ());
57
127
58
- query .getSingleResult ();
128
+ query .list ();
59
129
}
60
130
);
61
131
}
62
132
133
+
63
134
public static class CurrentApple {
64
135
private final int id ;
65
136
private final int pieId ;
@@ -70,12 +141,15 @@ public CurrentApple(int id, int pieId, int dir) {
70
141
this .pieId = pieId ;
71
142
this .dir = dir ;
72
143
}
144
+
73
145
public int getDir () {
74
146
return dir ;
75
147
}
148
+
76
149
public int getId () {
77
150
return id ;
78
151
}
152
+
79
153
public int getPieId () {
80
154
return pieId ;
81
155
}
@@ -93,20 +167,25 @@ public static class Apple {
93
167
public Integer getId () {
94
168
return id ;
95
169
}
170
+
96
171
public Apple setId (Integer id ) {
97
172
this .id = id ;
98
173
return this ;
99
174
}
175
+
100
176
public LocalDate getBakedOn () {
101
177
return bakedOn ;
102
178
}
179
+
103
180
public Apple setBakedOn (LocalDate bakedOn ) {
104
181
this .bakedOn = bakedOn ;
105
182
return this ;
106
183
}
184
+
107
185
public Pie getBakedPie () {
108
186
return bakedPie ;
109
187
}
188
+
110
189
public Apple setBakedPie (Pie bakedPie ) {
111
190
this .bakedPie = bakedPie ;
112
191
return this ;
@@ -123,13 +202,16 @@ public static class Pie {
123
202
public Integer getId () {
124
203
return id ;
125
204
}
205
+
126
206
public Pie setId (Integer id ) {
127
207
this .id = id ;
128
208
return this ;
129
209
}
210
+
130
211
public String getTaste () {
131
212
return taste ;
132
213
}
214
+
133
215
public Pie setTaste (String taste ) {
134
216
this .taste = taste ;
135
217
return this ;
0 commit comments