Skip to content

Commit 29da2c0

Browse files
mbladelbeikov
authored andcommitted
HHH-17379 HHH-17397 Add test for issue
1 parent 51d64b1 commit 29da2c0

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/query/LeftJoinNullnessPredicateQueryTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ public void testIsNotNull(SessionFactoryScope scope) {
8080
} );
8181
}
8282

83+
@Test
84+
public void testDereferenceIsNull(SessionFactoryScope scope) {
85+
scope.inTransaction( session -> {
86+
final List<Book> resultList = session.createQuery(
87+
"select book from Book book " +
88+
"left join book.author a " +
89+
"where a.id is null",
90+
Book.class
91+
).getResultList();
92+
assertThat( resultList ).hasSize( 1 );
93+
assertThat( resultList.get( 0 ).getTitle() ).isEqualTo( "Unknown Author" );
94+
} );
95+
}
96+
97+
@Test
98+
public void testDereferenceIsNotNull(SessionFactoryScope scope) {
99+
scope.inTransaction( session -> {
100+
final List<Book> resultList = session.createQuery(
101+
"select book from Book book " +
102+
"left join book.author a " +
103+
"where a.id is not null",
104+
Book.class
105+
).getResultList();
106+
assertThat( resultList ).hasSize( 2 );
107+
assertThat( resultList.stream().map( b -> b.title ) ).contains( "The Shining", "The Colour Out of Space" );
108+
} );
109+
}
110+
83111
@Test
84112
public void testIsNotNullWithCondition(SessionFactoryScope scope) {
85113
scope.inTransaction( session -> {
@@ -108,6 +136,34 @@ public void testIsNullWithCondition(SessionFactoryScope scope) {
108136
} );
109137
}
110138

139+
@Test
140+
public void testDereferenceIsNotWithCondition(SessionFactoryScope scope) {
141+
scope.inTransaction( session -> {
142+
final List<Book> resultList = session.createQuery(
143+
"select book from Book book " +
144+
"left join book.author a with a.alive = true " +
145+
"where a.id is not null",
146+
Book.class
147+
).getResultList();
148+
assertThat( resultList ).hasSize( 1 );
149+
assertThat( resultList.get( 0 ).getTitle() ).isEqualTo( "The Shining" );
150+
} );
151+
}
152+
153+
@Test
154+
public void testDereferenceIsNullWithCondition(SessionFactoryScope scope) {
155+
scope.inTransaction( session -> {
156+
final List<Book> resultList = session.createQuery(
157+
"select book from Book book " +
158+
"left join book.author a with a.alive = true " +
159+
"where a.id is null",
160+
Book.class
161+
).getResultList();
162+
assertThat( resultList ).hasSize( 2 );
163+
assertThat( resultList.stream().map( b -> b.title ) ).contains( "Unknown Author", "The Colour Out of Space" );
164+
} );
165+
}
166+
111167
@Entity( name = "Book" )
112168
public static class Book {
113169
@Id
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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 java.util.List;
10+
11+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.AfterAll;
18+
import org.junit.jupiter.api.BeforeAll;
19+
import org.junit.jupiter.api.Test;
20+
21+
import jakarta.persistence.Entity;
22+
import jakarta.persistence.Id;
23+
import jakarta.persistence.OneToOne;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* @author Marco Belladelli
29+
*/
30+
@DomainModel( annotatedClasses = {
31+
RightJoinNullnessPredicateQueryTest.RelatedEntity.class,
32+
RightJoinNullnessPredicateQueryTest.MainEntity.class,
33+
} )
34+
@SessionFactory
35+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17379" )
36+
public class RightJoinNullnessPredicateQueryTest {
37+
@BeforeAll
38+
public void setUp(SessionFactoryScope scope) {
39+
scope.inTransaction( session -> {
40+
final RelatedEntity related = new RelatedEntity( 1L );
41+
session.persist( related );
42+
session.persist( new RelatedEntity( 2L ) );
43+
session.persist( new MainEntity( 3L, related ) );
44+
session.persist( new MainEntity( 4L, null ) );
45+
} );
46+
}
47+
48+
@AfterAll
49+
public void tearDown(SessionFactoryScope scope) {
50+
scope.inTransaction( session -> {
51+
session.createMutationQuery( "delete from MainEntity" ).executeUpdate();
52+
session.createMutationQuery( "delete from RelatedEntity" ).executeUpdate();
53+
} );
54+
}
55+
56+
@Test
57+
public void testRightJoinIsNotNull(SessionFactoryScope scope) {
58+
scope.inTransaction( session -> {
59+
final List<Long> result = session.createQuery(
60+
"select r.id from MainEntity m right join m.related r where r is not null",
61+
Long.class
62+
).getResultList();
63+
assertThat( result ).hasSize( 2 );
64+
assertThat( result ).contains( 1L, 2L );
65+
} );
66+
}
67+
68+
@Test
69+
@RequiresDialectFeature( feature = DialectFeatureChecks.SupportsFullJoin.class )
70+
public void testFullJoinIsNull(SessionFactoryScope scope) {
71+
scope.inTransaction( session -> {
72+
final List<Long> result = session.createQuery(
73+
"select r.id from MainEntity m full join m.related r where r is null",
74+
Long.class
75+
).getResultList();
76+
assertThat( result ).hasSize( 1 );
77+
assertThat( result ).containsNull();
78+
} );
79+
}
80+
81+
@Test
82+
public void testDereferenceIsNotNull(SessionFactoryScope scope) {
83+
scope.inTransaction( session -> {
84+
final List<Long> result = session.createQuery(
85+
"select r.id from MainEntity m right join m.related r where r.id is not null",
86+
Long.class
87+
).getResultList();
88+
assertThat( result ).hasSize( 2 );
89+
assertThat( result ).contains( 1L, 2L );
90+
} );
91+
}
92+
93+
@Test
94+
@RequiresDialectFeature( feature = DialectFeatureChecks.SupportsFullJoin.class )
95+
public void testDereferenceIsNull(SessionFactoryScope scope) {
96+
scope.inTransaction( session -> {
97+
final List<Long> result = session.createQuery(
98+
"select r.id from MainEntity m full join m.related r where r.id is null",
99+
Long.class
100+
).getResultList();
101+
assertThat( result ).hasSize( 1 );
102+
assertThat( result ).containsNull();
103+
} );
104+
}
105+
106+
@Test
107+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17397" )
108+
public void testRightJoinCount(SessionFactoryScope scope) {
109+
scope.inTransaction( session -> {
110+
final Long result = session.createQuery(
111+
"select count(r) from MainEntity m right join m.related r",
112+
Long.class
113+
).getSingleResult();
114+
assertThat( result ).isEqualTo( 2L );
115+
} );
116+
}
117+
118+
@Test
119+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17397" )
120+
public void testDereferenceCount(SessionFactoryScope scope) {
121+
scope.inTransaction( session -> {
122+
final Long result = session.createQuery(
123+
"select count(r.id) from MainEntity m right join m.related r",
124+
Long.class
125+
).getSingleResult();
126+
assertThat( result ).isEqualTo( 2L );
127+
} );
128+
}
129+
130+
@Entity( name = "RelatedEntity" )
131+
public static class RelatedEntity {
132+
@Id
133+
private Long id;
134+
135+
public RelatedEntity() {
136+
}
137+
138+
public RelatedEntity(Long id) {
139+
this.id = id;
140+
}
141+
142+
public Long getId() {
143+
return id;
144+
}
145+
}
146+
147+
148+
@Entity( name = "MainEntity" )
149+
public static class MainEntity {
150+
@Id
151+
private Long id;
152+
153+
@OneToOne
154+
private RelatedEntity related;
155+
156+
public MainEntity() {
157+
}
158+
159+
public MainEntity(Long id, RelatedEntity related) {
160+
this.id = id;
161+
this.related = related;
162+
}
163+
164+
public Long getId() {
165+
return id;
166+
}
167+
168+
public RelatedEntity getRelated() {
169+
return related;
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)