|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.GeneratedValue; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import org.hibernate.dialect.PostgreSQLDialect; |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | + |
| 23 | +@DomainModel( |
| 24 | + annotatedClasses = { |
| 25 | + QuerySelectWhereArrayPositionTest.TestEntity.class, |
| 26 | + } |
| 27 | +) |
| 28 | +@SessionFactory |
| 29 | +@RequiresDialect( PostgreSQLDialect.class ) |
| 30 | +@JiraKey("HHH-19490") |
| 31 | +public class QuerySelectWhereArrayPositionTest { |
| 32 | + @BeforeEach |
| 33 | + public void setUp(SessionFactoryScope scope) { |
| 34 | + scope.inTransaction( |
| 35 | + session -> { |
| 36 | + session.persist(new TestEntity(List.of(1, 2, 3))); |
| 37 | + session.persist(new TestEntity(List.of(2, 3, 4))); |
| 38 | + session.persist(new TestEntity(List.of(3, 4, 5))); |
| 39 | + } |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testSelectWhereArrayPosition(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + List<TestEntity> result = session.createQuery( "select testEntity " |
| 47 | + + "from TestEntity testEntity " |
| 48 | + + "where ARRAY_POSITION(testEntity.values, ?1) IS NOT NULL", TestEntity.class ) |
| 49 | + .setParameter( 1, 3) |
| 50 | + .getResultList(); |
| 51 | + assertThat( result.size() ).isEqualTo( 3 ); |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + @Entity(name = "TestEntity") |
| 56 | + public static class TestEntity { |
| 57 | + |
| 58 | + @Id |
| 59 | + @GeneratedValue |
| 60 | + private Long id; |
| 61 | + |
| 62 | + private List<Integer> values; |
| 63 | + |
| 64 | + public TestEntity() { |
| 65 | + } |
| 66 | + |
| 67 | + public TestEntity(List<Integer> values) { |
| 68 | + this.values = values; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments