Skip to content

Commit e9fd4a6

Browse files
jrenaatsebersole
authored andcommitted
HHH-17493 - add tests and fix
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 5124966 commit e9fd4a6

File tree

5 files changed

+220
-6
lines changed

5 files changed

+220
-6
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/predicate/AbstractNegatableSqmPredicate.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ public void negate() {
4444
public SqmNegatablePredicate not() {
4545
// in certain cases JPA required that this always return
4646
// a new instance.
47-
if ( nodeBuilder().isJpaQueryComplianceEnabled() ) {
48-
return createNegatedNode();
49-
}
50-
51-
negate();
52-
return this;
47+
return createNegatedNode();
5348
}
5449

5550
}

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/predicate/SqmLikePredicate.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,10 @@ public void appendHqlString(StringBuilder sb) {
138138
protected SqmNegatablePredicate createNegatedNode() {
139139
return new SqmLikePredicate( matchExpression, pattern, escapeCharacter, !isNegated(), nodeBuilder() );
140140
}
141+
142+
@Override
143+
public SqmNegatablePredicate not() {
144+
negate();
145+
return this;
146+
}
141147
}

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/predicate/SqmNegatedPredicate.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ public void appendHqlString(StringBuilder sb) {
7979
protected SqmNegatablePredicate createNegatedNode() {
8080
return new SqmNegatedPredicate( this, nodeBuilder() );
8181
}
82+
8283
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.jpa.criteria.basic;
8+
9+
import java.util.List;
10+
11+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.persistence.criteria.CriteriaBuilder;
19+
import jakarta.persistence.criteria.CriteriaQuery;
20+
import jakarta.persistence.criteria.Root;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
25+
/**
26+
* @author Jan Schatteman
27+
*/
28+
@Jpa(
29+
annotatedClasses = {Wall.class},
30+
jpaComplianceEnabled = false
31+
)
32+
@JiraKey( value = "HHH-17493" )
33+
public class NegatedPredicateTest {
34+
35+
@BeforeEach
36+
public void setup(EntityManagerFactoryScope scope) {
37+
scope.inTransaction(
38+
entityManager -> {
39+
Wall wall1 = new Wall();
40+
wall1.setColor( "yellow" );
41+
Wall wall2 = new Wall();
42+
wall2.setColor( "green" );
43+
Wall wall3 = new Wall();
44+
wall3.setColor( "red" );
45+
entityManager.persist( wall1 );
46+
entityManager.persist( wall2 );
47+
entityManager.persist( wall3 );
48+
}
49+
);
50+
}
51+
52+
@AfterEach
53+
public void tearDown(EntityManagerFactoryScope scope) {
54+
scope.inTransaction(
55+
entityManager -> entityManager.createQuery( "delete from Wall" ).executeUpdate()
56+
);
57+
}
58+
59+
@Test
60+
public void testNegatedPredicate(EntityManagerFactoryScope scope) {
61+
scope.inEntityManager(
62+
entityManager -> {
63+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
64+
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
65+
Root<Wall> root = query.from( Wall.class );
66+
query.select( root ).where(
67+
cb.not(
68+
cb.or(
69+
cb.equal(root.get( "color" ), "yellow"),
70+
cb.equal(root.get( "color" ), "red")
71+
)
72+
)
73+
);
74+
Wall result = entityManager.createQuery( query ).getSingleResult();
75+
assertNotNull( result );
76+
assertEquals("green", result.getColor());
77+
}
78+
);
79+
}
80+
81+
@Test
82+
public void testDoubleNegatedPredicate(EntityManagerFactoryScope scope) {
83+
scope.inEntityManager(
84+
entityManager -> {
85+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
86+
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
87+
Root<Wall> root = query.from( Wall.class );
88+
query.select( root ).where(
89+
cb.not(
90+
cb.not(
91+
cb.or(
92+
cb.equal(root.get( "color" ), "yellow"),
93+
cb.equal(root.get( "color" ), "red")
94+
)
95+
)
96+
)
97+
);
98+
query.orderBy( cb.asc(root.get("id")) );
99+
List<Wall> result = entityManager.createQuery( query ).getResultList();
100+
assertEquals( 2, result.size() );
101+
assertEquals("yellow", result.get(0).getColor());
102+
assertEquals("red", result.get(1).getColor());
103+
}
104+
);
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.jpa.criteria.basic;
8+
9+
import java.util.List;
10+
11+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.persistence.criteria.CriteriaBuilder;
19+
import jakarta.persistence.criteria.CriteriaQuery;
20+
import jakarta.persistence.criteria.Root;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
25+
/**
26+
* @author Jan Schatteman
27+
*/
28+
@Jpa(
29+
annotatedClasses = {Wall.class},
30+
jpaComplianceEnabled = true
31+
)
32+
@JiraKey( value = "HHH-17493" )
33+
public class WithJpaComplianceNegatedPredicateTest {
34+
35+
@BeforeEach
36+
public void setup(EntityManagerFactoryScope scope) {
37+
scope.inTransaction(
38+
entityManager -> {
39+
Wall wall1 = new Wall();
40+
wall1.setColor( "yellow" );
41+
Wall wall2 = new Wall();
42+
wall2.setColor( "green" );
43+
Wall wall3 = new Wall();
44+
wall3.setColor( "red" );
45+
entityManager.persist( wall1 );
46+
entityManager.persist( wall2 );
47+
entityManager.persist( wall3 );
48+
}
49+
);
50+
}
51+
52+
@AfterEach
53+
public void tearDown(EntityManagerFactoryScope scope) {
54+
scope.inTransaction(
55+
entityManager -> entityManager.createQuery( "delete from Wall" ).executeUpdate()
56+
);
57+
}
58+
59+
@Test
60+
public void testNegatedPredicate(EntityManagerFactoryScope scope) {
61+
scope.inEntityManager(
62+
entityManager -> {
63+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
64+
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
65+
Root<Wall> root = query.from( Wall.class );
66+
query.select( root ).where(
67+
cb.not(
68+
cb.or(
69+
cb.equal(root.get( "color" ), "yellow"),
70+
cb.equal(root.get( "color" ), "red")
71+
)
72+
)
73+
);
74+
Wall result = entityManager.createQuery( query ).getSingleResult();
75+
assertNotNull( result );
76+
assertEquals("green", result.getColor());
77+
}
78+
);
79+
}
80+
81+
@Test
82+
public void testDoubleNegatedPredicate(EntityManagerFactoryScope scope) {
83+
scope.inEntityManager(
84+
entityManager -> {
85+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
86+
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
87+
Root<Wall> root = query.from( Wall.class );
88+
query.select( root ).where(
89+
cb.not(
90+
cb.not(
91+
cb.or(
92+
cb.equal(root.get( "color" ), "yellow"),
93+
cb.equal(root.get( "color" ), "red")
94+
)
95+
)
96+
)
97+
);
98+
query.orderBy( cb.asc(root.get("id")) );
99+
List<Wall> result = entityManager.createQuery( query ).getResultList();
100+
assertEquals( 2, result.size() );
101+
assertEquals("yellow", result.get(0).getColor());
102+
assertEquals("red", result.get(1).getColor());
103+
}
104+
);
105+
}
106+
}

0 commit comments

Comments
 (0)