Skip to content

Commit ab11020

Browse files
committed
HHH-18353 Add test for issue
1 parent de49475 commit ab11020

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.hibernate.orm.test.function.array;
2+
3+
import java.util.Arrays;
4+
5+
import org.hibernate.dialect.OracleDialect;
6+
7+
import org.hibernate.testing.jdbc.SharedDriverManagerTypeCacheClearingIntegrator;
8+
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
9+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.Jira;
12+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.ManyToOne;
22+
23+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
24+
25+
@DomainModel(
26+
annotatedClasses = {
27+
ArrayConstructorInSelectClauseTest.Book.class,
28+
ArrayConstructorInSelectClauseTest.Author.class
29+
})
30+
@SessionFactory
31+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsStructuralArrays.class)
32+
// Clear the type cache, otherwise we might run into ORA-21700: object does not exist or is marked for delete
33+
@BootstrapServiceRegistry(integrators = SharedDriverManagerTypeCacheClearingIntegrator.class)
34+
public class ArrayConstructorInSelectClauseTest {
35+
private static final Long AUTHOR_ID = 1l;
36+
private static final Long CO_AUTHOR_ID = 2l;
37+
private static final Long BOOK_ID = 3l;
38+
39+
@BeforeEach
40+
public void setUp(SessionFactoryScope scope) {
41+
scope.inTransaction(
42+
session -> {
43+
var dialect = session.getDialect();
44+
if ( dialect instanceof OracleDialect ) {
45+
session.createNativeQuery(
46+
"create or replace type LongArray as varying array(127) of number(19,0)" )
47+
.executeUpdate();
48+
}
49+
var author = new Author( AUTHOR_ID, "D Thomas" );
50+
var coAuthor = new Author( CO_AUTHOR_ID, "A Hunt" );
51+
session.persist( author );
52+
session.persist( coAuthor );
53+
54+
var book = new Book( BOOK_ID, "The Pragmatic Programmer", author, coAuthor );
55+
session.persist( book );
56+
}
57+
);
58+
}
59+
60+
@AfterEach
61+
public void tearDown(SessionFactoryScope scope) {
62+
scope.inTransaction(
63+
session -> {
64+
session.createMutationQuery( "delete from Book" ).executeUpdate();
65+
session.createMutationQuery( "delete from Author" ).executeUpdate();
66+
var dialect = session.getDialect();
67+
if ( dialect instanceof OracleDialect ) {
68+
session.createNativeQuery( "drop type LongArray" ).executeUpdate();
69+
}
70+
}
71+
);
72+
}
73+
74+
@Test
75+
@Jira("HHH-18353")
76+
public void testArraySelectionNoCast(SessionFactoryScope scope) {
77+
scope.inTransaction(
78+
session -> {
79+
var query = session.createQuery(
80+
"SELECT ARRAY(b.author.id, b.coAuthor.id) FROM Book b WHERE b.id = :bookId",
81+
Long[].class
82+
).setParameter( "bookId", BOOK_ID );
83+
84+
assertThat( query.getSingleResult() ).containsAll( Arrays.asList( AUTHOR_ID, CO_AUTHOR_ID ) );
85+
}
86+
);
87+
}
88+
89+
@Test
90+
public void testArraySelectionCast(SessionFactoryScope scope) {
91+
scope.inTransaction(
92+
session -> {
93+
var query = session.createQuery(
94+
"SELECT ARRAY(CAST(b.author.id as Long), CAST(b.coAuthor.id as Long)) FROM Book b WHERE b.id = :bookId",
95+
Long[].class
96+
).setParameter( "bookId", BOOK_ID );
97+
98+
assertThat( query.getSingleResult() ).containsAll( Arrays.asList( AUTHOR_ID, CO_AUTHOR_ID ) );
99+
}
100+
);
101+
}
102+
103+
@Entity(name = "Author")
104+
public static class Author {
105+
106+
@Id
107+
private Long id;
108+
109+
private String name;
110+
111+
public Author() {
112+
}
113+
114+
public Author(Long id, String name) {
115+
this.id = id;
116+
this.name = name;
117+
}
118+
}
119+
120+
@Entity(name = "Book")
121+
public static class Book {
122+
123+
@Id
124+
private Long id;
125+
126+
private String title;
127+
128+
@ManyToOne
129+
private Author author;
130+
131+
@ManyToOne
132+
private Author coAuthor;
133+
134+
public Book() {
135+
}
136+
137+
public Book(long id, String title, Author author, Author coAuthor) {
138+
this.id = id;
139+
this.title = title;
140+
this.author = author;
141+
this.coAuthor = coAuthor;
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)