Skip to content

Commit 8098830

Browse files
committed
HHH-16409 Add test for issue
1 parent c18391e commit 8098830

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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.query;
8+
9+
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.Jira;
11+
import org.hibernate.testing.orm.junit.SessionFactory;
12+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
13+
import org.junit.jupiter.api.AfterAll;
14+
import org.junit.jupiter.api.BeforeAll;
15+
import org.junit.jupiter.api.Test;
16+
17+
import jakarta.persistence.Column;
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.JoinColumn;
21+
import jakarta.persistence.ManyToOne;
22+
import jakarta.persistence.Tuple;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Marco Belladelli
28+
*/
29+
@SessionFactory
30+
@DomainModel( annotatedClasses = {
31+
EntityValuedPathsGroupByOrderByTest.EntityA.class,
32+
EntityValuedPathsGroupByOrderByTest.EntityB.class
33+
} )
34+
@Jira( "https://hibernate.atlassian.net/browse/HHH-16409" )
35+
public class EntityValuedPathsGroupByOrderByTest {
36+
@BeforeAll
37+
public void setUp(SessionFactoryScope scope) {
38+
scope.inTransaction( session -> {
39+
final EntityB entityB = new EntityB( 1L, "entity_b" );
40+
session.persist( entityB );
41+
session.persist( new EntityA( 2L, 1, entityB ) );
42+
session.persist( new EntityA( 3L, 2, entityB ) );
43+
} );
44+
}
45+
46+
@AfterAll
47+
public void tearDown(SessionFactoryScope scope) {
48+
scope.inTransaction( session -> {
49+
session.createMutationQuery( "delete from EntityA" ).executeUpdate();
50+
session.createMutationQuery( "delete from EntityB" ).executeUpdate();
51+
} );
52+
}
53+
54+
// Root ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+
56+
@Test
57+
public void testRootGroupBy(SessionFactoryScope scope) {
58+
scope.inTransaction( session -> assertThat( session.createQuery(
59+
"select b.name from EntityB b group by b",
60+
String.class
61+
).getSingleResult() ).isEqualTo( "entity_b" ) );
62+
}
63+
64+
@Test
65+
public void testRootGroupByAndOrderBy(SessionFactoryScope scope) {
66+
scope.inTransaction( session -> assertThat( session.createQuery(
67+
"select b.name from EntityB b group by b order by b",
68+
String.class
69+
).getSingleResult() ).isEqualTo( "entity_b" ) );
70+
}
71+
72+
@Test
73+
public void testRootSelectAndGroupBy(SessionFactoryScope scope) {
74+
scope.inTransaction( session -> assertThat( session.createQuery(
75+
"select b from EntityB b group by b",
76+
EntityB.class
77+
).getSingleResult().getName() ).isEqualTo( "entity_b" ) );
78+
}
79+
80+
@Test
81+
public void testRootSelectAndGroupByAndOrderBy(SessionFactoryScope scope) {
82+
scope.inTransaction( session -> assertThat( session.createQuery(
83+
"select b from EntityB b group by b order by b",
84+
EntityB.class
85+
).getSingleResult().getName() ).isEqualTo( "entity_b" ) );
86+
}
87+
88+
// Explicit join ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
@Test
91+
public void testJoinGroupBy(SessionFactoryScope scope) {
92+
scope.inTransaction( session -> assertThat( session.createQuery(
93+
"select b.name, sum(a.amount) from EntityA a join a.secondary b group by b order by b.name",
94+
Tuple.class
95+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
96+
}
97+
98+
@Test
99+
public void testJoinGroupByAndOrderBy(SessionFactoryScope scope) {
100+
scope.inTransaction( session -> assertThat( session.createQuery(
101+
"select b.name, sum(a.amount) from EntityA a join a.secondary b group by b order by b",
102+
Tuple.class
103+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
104+
}
105+
106+
@Test
107+
public void testJoinSelectAndGroupBy(SessionFactoryScope scope) {
108+
scope.inTransaction( session -> assertThat( session.createQuery(
109+
"select b, sum(a.amount) from EntityA a join a.secondary b group by b order by b.name",
110+
Tuple.class
111+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
112+
}
113+
114+
@Test
115+
public void testJoinSelectAndGroupByAndOrderBy(SessionFactoryScope scope) {
116+
scope.inTransaction( session -> assertThat( session.createQuery(
117+
"select b, sum(a.amount) from EntityA a join a.secondary b group by b order by b",
118+
Tuple.class
119+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
120+
}
121+
122+
// Implicit join ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123+
124+
@Test
125+
public void testImplicitJoinGroupBy(SessionFactoryScope scope) {
126+
scope.inTransaction( session -> assertThat( session.createQuery(
127+
"select a.secondary.name, sum(a.amount) from EntityA a group by a.secondary order by a.secondary.name",
128+
Tuple.class
129+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
130+
}
131+
132+
@Test
133+
public void testImplicitJoinGroupByAndOrderBy(SessionFactoryScope scope) {
134+
scope.inTransaction( session -> assertThat( session.createQuery(
135+
"select a.secondary.name, sum(a.amount) from EntityA a group by a.secondary order by a.secondary",
136+
Tuple.class
137+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
138+
}
139+
140+
@Test
141+
public void testImplicitJoinSelectAndGroupBy(SessionFactoryScope scope) {
142+
scope.inTransaction( session -> assertThat( session.createQuery(
143+
"select a.secondary, sum(a.amount) from EntityA a group by a.secondary",
144+
Tuple.class
145+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
146+
}
147+
148+
@Test
149+
public void testImplicitJoinSelectAndGroupByAndOrderBy(SessionFactoryScope scope) {
150+
scope.inTransaction( session -> assertThat( session.createQuery(
151+
"select a.secondary, sum(a.amount) from EntityA a group by a.secondary order by a.secondary",
152+
Tuple.class
153+
).getSingleResult().get( 1 ) ).isEqualTo( 3L ) );
154+
}
155+
156+
@Entity( name = "EntityA" )
157+
public static class EntityA {
158+
@Id
159+
private Long id;
160+
161+
private Integer amount;
162+
163+
@ManyToOne
164+
@JoinColumn( name = "secondary_id" )
165+
private EntityB secondary;
166+
167+
public EntityA() {
168+
}
169+
170+
public EntityA(Long id, Integer amount, EntityB secondary) {
171+
this.id = id;
172+
this.amount = amount;
173+
this.secondary = secondary;
174+
}
175+
176+
public Long getId() {
177+
return id;
178+
}
179+
180+
public Integer getAmount() {
181+
return amount;
182+
}
183+
184+
public EntityB getSecondary() {
185+
return secondary;
186+
}
187+
188+
}
189+
190+
@Entity( name = "EntityB" )
191+
public static class EntityB {
192+
@Id
193+
@Column( name = "id_col" )
194+
private Long id;
195+
196+
@Column( name = "name_col" )
197+
private String name;
198+
199+
public EntityB() {
200+
}
201+
202+
public EntityB(Long id, String name) {
203+
this.id = id;
204+
this.name = name;
205+
}
206+
207+
public Long getId() {
208+
return id;
209+
}
210+
211+
public String getName() {
212+
return name;
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)