HHH-17493 - add tests and fix

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2024-03-20 15:20:36 +01:00 committed by Christian Beikov
parent c1af23867d
commit 089a935cb6
5 changed files with 220 additions and 6 deletions

View File

@ -44,12 +44,7 @@ public abstract class AbstractNegatableSqmPredicate extends AbstractSqmPredicate
public SqmNegatablePredicate not() {
// in certain cases JPA required that this always return
// a new instance.
if ( nodeBuilder().isJpaQueryComplianceEnabled() ) {
return createNegatedNode();
}
negate();
return this;
return createNegatedNode();
}
}

View File

@ -145,4 +145,10 @@ public class SqmLikePredicate extends AbstractNegatableSqmPredicate {
protected SqmNegatablePredicate createNegatedNode() {
return new SqmLikePredicate( matchExpression, pattern, escapeCharacter, !isNegated(), nodeBuilder() );
}
@Override
public SqmNegatablePredicate not() {
negate();
return this;
}
}

View File

@ -79,4 +79,5 @@ public class SqmNegatedPredicate extends AbstractNegatableSqmPredicate {
protected SqmNegatablePredicate createNegatedNode() {
return new SqmNegatedPredicate( this, nodeBuilder() );
}
}

View File

@ -0,0 +1,106 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.jpa.criteria.basic;
import java.util.List;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author Jan Schatteman
*/
@Jpa(
annotatedClasses = {Wall.class},
jpaComplianceEnabled = false
)
@JiraKey( value = "HHH-17493" )
public class NegatedPredicateTest {
@BeforeEach
public void setup(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Wall wall1 = new Wall();
wall1.setColor( "yellow" );
Wall wall2 = new Wall();
wall2.setColor( "green" );
Wall wall3 = new Wall();
wall3.setColor( "red" );
entityManager.persist( wall1 );
entityManager.persist( wall2 );
entityManager.persist( wall3 );
}
);
}
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> entityManager.createQuery( "delete from Wall" ).executeUpdate()
);
}
@Test
public void testNegatedPredicate(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.or(
cb.equal(root.get( "color" ), "yellow"),
cb.equal(root.get( "color" ), "red")
)
)
);
Wall result = entityManager.createQuery( query ).getSingleResult();
assertNotNull( result );
assertEquals("green", result.getColor());
}
);
}
@Test
public void testDoubleNegatedPredicate(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.not(
cb.or(
cb.equal(root.get( "color" ), "yellow"),
cb.equal(root.get( "color" ), "red")
)
)
)
);
query.orderBy( cb.asc(root.get("id")) );
List<Wall> result = entityManager.createQuery( query ).getResultList();
assertEquals( 2, result.size() );
assertEquals("yellow", result.get(0).getColor());
assertEquals("red", result.get(1).getColor());
}
);
}
}

View File

@ -0,0 +1,106 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.jpa.criteria.basic;
import java.util.List;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author Jan Schatteman
*/
@Jpa(
annotatedClasses = {Wall.class},
jpaComplianceEnabled = true
)
@JiraKey( value = "HHH-17493" )
public class WithJpaComplianceNegatedPredicateTest {
@BeforeEach
public void setup(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Wall wall1 = new Wall();
wall1.setColor( "yellow" );
Wall wall2 = new Wall();
wall2.setColor( "green" );
Wall wall3 = new Wall();
wall3.setColor( "red" );
entityManager.persist( wall1 );
entityManager.persist( wall2 );
entityManager.persist( wall3 );
}
);
}
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> entityManager.createQuery( "delete from Wall" ).executeUpdate()
);
}
@Test
public void testNegatedPredicate(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.or(
cb.equal(root.get( "color" ), "yellow"),
cb.equal(root.get( "color" ), "red")
)
)
);
Wall result = entityManager.createQuery( query ).getSingleResult();
assertNotNull( result );
assertEquals("green", result.getColor());
}
);
}
@Test
public void testDoubleNegatedPredicate(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Wall> query = cb.createQuery( Wall.class );
Root<Wall> root = query.from( Wall.class );
query.select( root ).where(
cb.not(
cb.not(
cb.or(
cb.equal(root.get( "color" ), "yellow"),
cb.equal(root.get( "color" ), "red")
)
)
)
);
query.orderBy( cb.asc(root.get("id")) );
List<Wall> result = entityManager.createQuery( query ).getResultList();
assertEquals( 2, result.size() );
assertEquals("yellow", result.get(0).getColor());
assertEquals("red", result.get(1).getColor());
}
);
}
}