|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.function.array; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Tuple; |
| 10 | +import org.hibernate.dialect.H2Dialect; |
| 11 | +import org.hibernate.dialect.HSQLDialect; |
| 12 | +import org.hibernate.query.criteria.JpaCriteriaQuery; |
| 13 | +import org.hibernate.query.criteria.JpaCteCriteria; |
| 14 | +import org.hibernate.query.criteria.JpaRoot; |
| 15 | +import org.hibernate.query.sqm.NodeBuilder; |
| 16 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsArrayToString; |
| 17 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsJsonArrayAgg; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 23 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Kowsar Atazadeh |
| 34 | + */ |
| 35 | +@DomainModel( |
| 36 | + annotatedClasses = {ArrayToStringWithArrayAggregateTest.Book.class, ArrayToStringWithArrayAggregateTest.Dummy.class}) |
| 37 | +@SessionFactory |
| 38 | +@RequiresDialectFeature(feature = SupportsArrayToString.class) |
| 39 | +@RequiresDialectFeature( feature = SupportsJsonArrayAgg.class) |
| 40 | +@SkipForDialect(dialectClass = H2Dialect.class, reason = "Generated SQL query is not correct") |
| 41 | +@SkipForDialect(dialectClass = HSQLDialect.class, reason = "Generated SQL query is not correct") |
| 42 | +@JiraKey("HHH-18981") |
| 43 | +public class ArrayToStringWithArrayAggregateTest { |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void prepareData(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( em -> { |
| 48 | + em.persist( new Book( 1, "title1" ) ); |
| 49 | + em.persist( new Book( 2, "title2" ) ); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + public void cleanup(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( em -> { |
| 56 | + em.createMutationQuery( "delete from Book" ).executeUpdate(); |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void test(SessionFactoryScope scope) { |
| 62 | + scope.inSession( em -> { |
| 63 | + final NodeBuilder cb = (NodeBuilder) em.getCriteriaBuilder(); |
| 64 | + final JpaCriteriaQuery<Tuple> q = cb.createTupleQuery(); |
| 65 | + JpaRoot<Book> root = q.from( Book.class ); |
| 66 | + |
| 67 | + q.multiselect( |
| 68 | + cb.arrayToString( |
| 69 | + cb.arrayAgg( cb.asc( root.get( "title" ) ), root.get( "title" ) ), |
| 70 | + "," |
| 71 | + ).alias( "titles" ) |
| 72 | + ); |
| 73 | + List<Tuple> list = em.createQuery( q ).getResultList(); |
| 74 | + String titles = list.get( 0 ).get( "titles", String.class ); |
| 75 | + assertThat( titles ).isEqualTo( "title1,title2" ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testWithCte(SessionFactoryScope scope) { |
| 81 | + scope.inSession( em -> { |
| 82 | + final NodeBuilder cb = (NodeBuilder) em.getCriteriaBuilder(); |
| 83 | + |
| 84 | + JpaCriteriaQuery<Tuple> cteQuery = cb.createTupleQuery(); |
| 85 | + JpaRoot<Book> cteRoot = cteQuery.from( Book.class ); |
| 86 | + cteQuery.multiselect( |
| 87 | + cb.arrayAgg( cb.asc( cteRoot.get( "title" ) ), cteRoot.get( "title" ) ) |
| 88 | + .alias( "titles_array" ) |
| 89 | + ); |
| 90 | + |
| 91 | + JpaCriteriaQuery<Tuple> query = cb.createTupleQuery(); |
| 92 | + JpaCteCriteria<Tuple> titlesCte = query.with( cteQuery ); |
| 93 | + JpaRoot<Tuple> root = query.from( titlesCte ); |
| 94 | + query.multiselect( |
| 95 | + cb.arrayToString( root.get( "titles_array" ), cb.literal( "," ) ) |
| 96 | + .alias( "titles" ) |
| 97 | + ); |
| 98 | + |
| 99 | + List<Tuple> list = em.createQuery( query ).getResultList(); |
| 100 | + String titles = list.get( 0 ).get( "titles", String.class ); |
| 101 | + assertThat( titles ).isEqualTo( "title1,title2" ); |
| 102 | + } ); |
| 103 | + } |
| 104 | + |
| 105 | + @Entity(name = "Book") |
| 106 | + public static class Book { |
| 107 | + @Id |
| 108 | + private Integer id; |
| 109 | + private String title; |
| 110 | + |
| 111 | + public Book() { |
| 112 | + } |
| 113 | + |
| 114 | + public Book(Integer id, String title) { |
| 115 | + this.id = id; |
| 116 | + this.title = title; |
| 117 | + } |
| 118 | + |
| 119 | + public Integer getId() { |
| 120 | + return id; |
| 121 | + } |
| 122 | + |
| 123 | + public void setId(Integer id) { |
| 124 | + this.id = id; |
| 125 | + } |
| 126 | + |
| 127 | + public String getTitle() { |
| 128 | + return title; |
| 129 | + } |
| 130 | + |
| 131 | + public void setTitle(String title) { |
| 132 | + this.title = title; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Needed for Oracle |
| 137 | + @Entity |
| 138 | + static class Dummy { |
| 139 | + @Id |
| 140 | + Long id; |
| 141 | + String[] theArray; |
| 142 | + } |
| 143 | +} |
0 commit comments