Skip to content

Commit b9e47aa

Browse files
committed
HHH-18816 Add test for issue
1 parent 556285b commit b9e47aa

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.query.hql;
6+
7+
import org.hibernate.dialect.DerbyDialect;
8+
import org.hibernate.dialect.HSQLDialect;
9+
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.Jira;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.hibernate.testing.orm.junit.SkipForDialect;
15+
import org.junit.jupiter.api.AfterAll;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
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+
@DomainModel(annotatedClasses = {
30+
ExistsSubqueryForeignKeyTest.Person.class,
31+
ExistsSubqueryForeignKeyTest.Document.class,
32+
})
33+
@SessionFactory
34+
@SkipForDialect(dialectClass = HSQLDialect.class, reason = "HSQLDB doesn't like the case-when selection not being in the group-by")
35+
@SkipForDialect(dialectClass = DerbyDialect.class, reason = "Derby doesn't like the case-when selection not being in the group-by")
36+
@Jira( "https://hibernate.atlassian.net/browse/HHH-18816" )
37+
public class ExistsSubqueryForeignKeyTest {
38+
@Test
39+
public void testWhereClause(SessionFactoryScope scope) {
40+
scope.inTransaction( session -> {
41+
final Long result = session.createQuery(
42+
"select count(*) from Document d join d.owner o "
43+
+ "where exists(select p.id from Person p where p.id = o.id) group by o.id",
44+
Long.class
45+
).getSingleResult();
46+
assertThat( result ).isEqualTo( 1L );
47+
} );
48+
}
49+
50+
@Test
51+
public void testSelectCaseWhen(SessionFactoryScope scope) {
52+
scope.inTransaction( session -> {
53+
final Tuple result = session.createQuery(
54+
"select case when exists(select p.id from Person p where p.id = o.id) then 1 else 0 end,"
55+
+ "count(*) from Document d join d.owner o group by o.id",
56+
Tuple.class
57+
).getSingleResult();
58+
assertThat( result.get( 0, Integer.class ) ).isEqualTo( 1 );
59+
assertThat( result.get( 1, Long.class ) ).isEqualTo( 1L );
60+
} );
61+
}
62+
63+
@BeforeAll
64+
public void setUp(SessionFactoryScope scope) {
65+
scope.inTransaction( session -> {
66+
final Person person1 = new Person( 1L, "person_1" );
67+
session.persist( person1 );
68+
session.persist( new Document( 1L, "doc_1", person1 ) );
69+
} );
70+
}
71+
72+
@AfterAll
73+
public void tearDown(SessionFactoryScope scope) {
74+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
75+
}
76+
77+
@Entity(name = "Person")
78+
static class Person {
79+
@Id
80+
private Long id;
81+
82+
private String name;
83+
84+
public Person() {
85+
}
86+
87+
public Person(Long id, String name) {
88+
this.id = id;
89+
this.name = name;
90+
}
91+
}
92+
93+
@Entity(name = "Document")
94+
static class Document {
95+
@Id
96+
private Long id;
97+
98+
private String title;
99+
100+
@ManyToOne
101+
private Person owner;
102+
103+
public Document() {
104+
}
105+
106+
public Document(Long id, String title, Person owner) {
107+
this.id = id;
108+
this.title = title;
109+
this.owner = owner;
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)