-
Notifications
You must be signed in to change notification settings - Fork 3.6k
HHH-19497 Test for composition of 'not' 'in' restrictions #10412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelfranz
wants to merge
2
commits into
hibernate:main
Choose a base branch
from
michaelfranz:HHH-19497-negated-in-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/basic/Foo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.jpa.criteria.basic; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.JoinColumns; | ||
import jakarta.persistence.ManyToOne; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Entity used in <code>org.hibernate.orm.test.jpa.criteria.basic.NegatedInPredicateTest</code>. | ||
* | ||
* @author Mike Mannion | ||
*/ | ||
@Entity | ||
public class Foo { | ||
|
||
@Id | ||
Long id; | ||
|
||
@ManyToOne | ||
@JoinColumns({ | ||
@JoinColumn(name = "FK_CODE", referencedColumnName = "CODE"), | ||
@JoinColumn(name = "FK_CONTEXT", referencedColumnName = "CONTEXT") | ||
}) | ||
FooType fooType; | ||
|
||
public Foo() { | ||
// For JPA | ||
} | ||
|
||
public Foo(Long id, FooType fooType) { | ||
this.id = id; | ||
this.fooType = fooType; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Foo customer = (Foo) o; | ||
return Objects.equals(id, customer.id) && Objects.equals(fooType, customer.fooType); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hashCode(id); | ||
result = 31 * result + Objects.hashCode(fooType); | ||
return result; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/basic/FooType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.jpa.criteria.basic; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
/** | ||
* Entity used in <code>org.hibernate.orm.test.jpa.criteria.basic.NegatedInPredicateTest</code>. | ||
* | ||
* @author Mike Mannion | ||
*/ | ||
@Entity | ||
public class FooType { | ||
@Id | ||
@Column(name = "CODE") | ||
String code; | ||
|
||
@Column(name = "CONTEXT") | ||
String context; | ||
|
||
public FooType() { | ||
// For JPA | ||
} | ||
|
||
FooType(String code, String context) { | ||
this.code = code; | ||
this.context = context; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...-core/src/test/java/org/hibernate/orm/test/jpa/criteria/basic/NegatedInPredicateTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.jpa.criteria.basic; | ||
|
||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.Root; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
/** | ||
* Add test which composes not with in. Test introduced after discovery the negated in | ||
* fails under dialects without record-level construction, such as DB2. | ||
* | ||
* @author Mike Mannion | ||
*/ | ||
@JiraKey(value = "HHH-19497") | ||
@DomainModel( | ||
annotatedClasses = {Foo.class, FooType.class} | ||
) | ||
@SessionFactory | ||
class NegatedInPredicateTest { | ||
|
||
public static final FooType FOO_TYPE1 = new FooType( "ft1", "ctx1" ); | ||
public static final FooType FOO_TYPE2 = new FooType( "ft2", "ctx1" ); | ||
|
||
@BeforeEach | ||
void setup(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
em -> { | ||
em.persist( FOO_TYPE1 ); | ||
em.persist( FOO_TYPE2 ); | ||
|
||
Foo foo1 = new Foo( 1L, FOO_TYPE1 ); | ||
Foo foo2 = new Foo( 2L, FOO_TYPE1 ); | ||
Foo foo3 = new Foo( 3L, FOO_TYPE2 ); | ||
|
||
em.persist( foo1 ); | ||
em.persist( foo2 ); | ||
em.persist( foo3 ); | ||
} | ||
); | ||
} | ||
|
||
@AfterEach | ||
void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
em -> { | ||
em.createQuery( "delete from Foo" ).executeUpdate(); | ||
em.createQuery( "delete from FooType" ).executeUpdate(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
void testSanity(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
em -> { | ||
CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); | ||
Root<Foo> root = cq.from( Foo.class ); | ||
assertThat( em.createQuery( cq.select( root ) ).getResultList() ) | ||
.hasSize( 3 ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
void testNegatedPredicate(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
em -> { | ||
CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); | ||
Root<Foo> root = cq.from( Foo.class ); | ||
cq.select( root ) | ||
.where( cb.not( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) ) ); | ||
assertThat( em.createQuery( cq ).getResultList() ) | ||
.isEmpty(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
void testNonNegatedInPredicate(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
em -> { | ||
CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); | ||
Root<Foo> root = cq.from( Foo.class ); | ||
cq.select( root ) | ||
.where( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) ); | ||
assertThat( em.createQuery( cq ).getResultList() ) | ||
.hasSize( 3 ); | ||
|
||
} | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix and test. I'm just curious why you didn't change the junction type here instead. Am I missing some subtle case where using
not ( (x=x1 and y=y1) or (x=x2 and y=y2) )
is "more" correct over( (x<>x1 or y<>y1) and (x<>x2 or y<>y2) )
?I also did some testing with
null
values on PostgreSQL and to me it seems that both kinds of expressions are semantically equivalent. With an increasing amount of in-list items (3+), thenot
approach is more compact, which is why I would even prefer that. Since you seem to be on DB2, could you please let me know if there is a performance difference between the two predicate styles i.e. are the query execution plans the same?