Skip to content

Commit 8e5f847

Browse files
mbelladebeikov
authored andcommitted
HHH-17490 Fix not in and empty list parameter predicate
1 parent cfe41ed commit 8e5f847

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ else if ( expression instanceof Summarization ) {
201201
public void visitInListPredicate(InListPredicate inListPredicate) {
202202
final List<Expression> listExpressions = inListPredicate.getListExpressions();
203203
if ( listExpressions.isEmpty() ) {
204-
appendSql( "1=0" );
204+
appendSql( "1=" + ( inListPredicate.isNegated() ? "1" : "0" ) );
205205
return;
206206
}
207207
final Expression testExpression = inListPredicate.getTestExpression();

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/FirebirdSqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ else if ( expression instanceof Summarization ) {
229229
public void visitInListPredicate(InListPredicate inListPredicate) {
230230
final List<Expression> listExpressions = inListPredicate.getListExpressions();
231231
if ( listExpressions.isEmpty() ) {
232-
appendSql( "1=0" );
232+
appendSql( "1=" + ( inListPredicate.isNegated() ? "1" : "0" ) );
233233
return;
234234
}
235235
final Expression testExpression = inListPredicate.getTestExpression();

hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ else if ( expression instanceof Summarization ) {
199199
public void visitInListPredicate(InListPredicate inListPredicate) {
200200
final List<Expression> listExpressions = inListPredicate.getListExpressions();
201201
if ( listExpressions.isEmpty() ) {
202-
appendSql( "1=0" );
202+
appendSql( "1=" + ( inListPredicate.isNegated() ? "1" : "0" ) );
203203
return;
204204
}
205205
final Expression testExpression = inListPredicate.getTestExpression();

hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6957,7 +6957,7 @@ public void visitGroupedPredicate(GroupedPredicate groupedPredicate) {
69576957
public void visitInListPredicate(InListPredicate inListPredicate) {
69586958
final List<Expression> listExpressions = inListPredicate.getListExpressions();
69596959
if ( listExpressions.isEmpty() ) {
6960-
appendSql( "1=0" );
6960+
appendSql( "1=" + ( inListPredicate.isNegated() ? "1" : "0" ) );
69616961
return;
69626962
}
69636963
Function<Expression, Expression> itemAccessor = Function.identity();

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/QueryTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.persistence.Query;
2323
import jakarta.persistence.TemporalType;
2424
import jakarta.persistence.Tuple;
25+
import jakarta.persistence.TypedQuery;
2526

2627
import org.hibernate.Hibernate;
2728
import org.hibernate.QueryException;
@@ -38,6 +39,7 @@
3839

3940
import org.hibernate.testing.SkipForDialect;
4041
import org.hibernate.testing.TestForIssue;
42+
import org.hibernate.testing.orm.junit.Jira;
4143
import org.junit.Test;
4244
import junit.framework.Assert;
4345

@@ -894,6 +896,54 @@ public void testParameterListInExistingParens() throws Exception {
894896
}
895897
}
896898

899+
@Test
900+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17490" )
901+
public void testEmptyParameterList() throws Exception {
902+
final Item item = new Item( "Mouse", "Micro$oft mouse" );
903+
final Item item2 = new Item( "Computer", "Dell computer" );
904+
905+
EntityManager em = getOrCreateEntityManager();
906+
em.getTransaction().begin();
907+
try {
908+
em.persist( item );
909+
em.persist( item2 );
910+
assertTrue( em.contains( item ) );
911+
em.getTransaction().commit();
912+
913+
em.getTransaction().begin();
914+
TypedQuery<Item> q = em.createQuery(
915+
"select item from Item item where item.name in :names",
916+
Item.class
917+
);
918+
q.setParameter( "names", List.of() );
919+
List<Item> result = q.getResultList();
920+
assertNotNull( result );
921+
assertEquals( 0, result.size() );
922+
923+
q = em.createQuery(
924+
"select item from Item item where item.name not in :names",
925+
Item.class
926+
);
927+
q.setParameter( "names", List.of() );
928+
result = q.getResultList();
929+
assertNotNull( result );
930+
assertEquals( 2, result.size() );
931+
932+
em.remove( result.get( 0 ) );
933+
em.remove( result.get( 1 ) );
934+
em.getTransaction().commit();
935+
}
936+
catch (Exception e){
937+
if ( em.getTransaction() != null && em.getTransaction().isActive() ) {
938+
em.getTransaction().rollback();
939+
}
940+
throw e;
941+
}
942+
finally {
943+
em.close();
944+
}
945+
}
946+
897947
@Test
898948
public void testEscapeCharacter() throws Exception {
899949
final Item item = new Item( "Mouse", "Micro_oft mouse" );

0 commit comments

Comments
 (0)