From df6ec5697869413919e3cf94ce99b476835641bb Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 28 Feb 2023 18:13:59 +0100 Subject: [PATCH] HHH-15895 Add test for issue --- .../test/jpa/criteria/InPredicateTest.java | 98 +++++++++++++++++++ .../orm/junit/EntityManagerFactoryScope.java | 5 + 2 files changed, 103 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/InPredicateTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/InPredicateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/InPredicateTest.java new file mode 100644 index 0000000000..ab4a7534b1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/InPredicateTest.java @@ -0,0 +1,98 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.dialect.Dialect; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.dialect.HSQLDialect; +import org.hibernate.dialect.MariaDBDialect; +import org.hibernate.dialect.MySQLDialect; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Jpa(annotatedClasses = { + InPredicateTest.Event.class +}) +@TestForIssue(jiraKey = "HHH-15895") +public class InPredicateTest { + + @Test + public void testInPredicate(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery cr = cb.createQuery( Event.class ); + Root root = cr.from( Event.class ); + List names = getNames( scope ); + cr.select( root ).where( root.get( "name" ).in( names ) ); + + List results = entityManager.createQuery( cr ).getResultList(); + assertNotNull( results ); + } + ); + } + + private List getNames(EntityManagerFactoryScope scope) { + int maxNames; + Dialect dialect = scope.getDialect(); + if ( dialect instanceof H2Dialect + || dialect instanceof MariaDBDialect + || dialect instanceof HSQLDialect + || dialect instanceof MySQLDialect ) { + maxNames = 100000; + } + else { + // the other dialects does not support 100000 parameters + maxNames = 65500; + } + + List names = new ArrayList<>( maxNames ); + for ( int i = 0; i < maxNames; i++ ) { + names.add( "abc" + i ); + } + return names; + } + + @Entity(name = "Event") + @Table(name = "EVENT_TABLE") + public static class Event { + + @Id + @GeneratedValue + private Long id; + + private String name; + + public Event() { + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/EntityManagerFactoryScope.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/EntityManagerFactoryScope.java index dd025455f8..0fdab41459 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/EntityManagerFactoryScope.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/EntityManagerFactoryScope.java @@ -11,6 +11,8 @@ import java.util.function.Function; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector; @@ -34,5 +36,8 @@ public interface EntityManagerFactoryScope { T fromTransaction(Function action); T fromTransaction(EntityManager entityManager, Function action); + default Dialect getDialect() { + return ((SessionFactoryImplementor) getEntityManagerFactory()).getJdbcServices().getDialect(); + } }