Skip to content

Commit 3bac078

Browse files
committed
HHH-18816 Add test for issue
1 parent f348207 commit 3bac078

File tree

1 file changed

+110
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)