|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.sql; |
| 6 | + |
| 7 | + |
| 8 | +import org.hibernate.orm.test.sql.hand.Person; |
| 9 | +import org.hibernate.query.NativeQuery; |
| 10 | +import org.hibernate.testing.orm.domain.StandardDomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.Jira; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.hibernate.testing.orm.domain.library.Book; |
| 16 | +import org.hibernate.type.StandardBasicTypes; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Jan Schatteman |
| 25 | + */ |
| 26 | +@DomainModel( |
| 27 | + standardModels = StandardDomainModel.LIBRARY |
| 28 | +) |
| 29 | +@SessionFactory |
| 30 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19383") |
| 31 | +public class NativeQueryResultCheckingTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + @Jira("https://hibernate.atlassian.net/browse/HHH-19376") |
| 35 | + public void testForHHH19376(SessionFactoryScope scope) { |
| 36 | + scope.inTransaction( |
| 37 | + session -> { |
| 38 | + String sql = "SELECT p.*, COUNT(*) OVER() AS total_count " |
| 39 | + + "FROM Person p " |
| 40 | + + "WHERE p.name ILIKE :name " |
| 41 | + + "ORDER BY p.id"; |
| 42 | + // Declare Person as result type |
| 43 | + NativeQuery<Person> query = session.createNativeQuery(sql, Person.class); |
| 44 | + query.setParameter("name", "Ga%"); |
| 45 | + query.setMaxResults(2); |
| 46 | + query.setFirstResult(0); |
| 47 | + // Now mutate the result set mapping and verify an Exception is thrown |
| 48 | + assertThrows( IllegalArgumentException.class, |
| 49 | + () -> query.addScalar( "total_count", StandardBasicTypes.LONG).list() ); |
| 50 | + } |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testMutateResultSetMapping1(SessionFactoryScope scope) { |
| 56 | + scope.inTransaction( |
| 57 | + session -> |
| 58 | + assertThrows( IllegalArgumentException.class, |
| 59 | + () -> session.createNativeQuery( "select title, isbn from Book", String.class ) |
| 60 | + .addScalar( "title", String.class ) |
| 61 | + .addScalar( "isbn", String.class ) |
| 62 | + .getResultList() |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testMutateResultSetMapping2(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( |
| 70 | + session -> |
| 71 | + assertThrows( IllegalArgumentException.class, |
| 72 | + () -> session.createNativeQuery( "select title, isbn from Book", Book.class ) |
| 73 | + // this mapping doesn't have an appropriate constructor in Book, should throw error |
| 74 | + .addScalar( "title", String.class ) |
| 75 | + .addScalar( "isbn", String.class ) |
| 76 | + .getResultList() |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testRecordWithPrimitiveField(SessionFactoryScope scope) { |
| 83 | + scope.inTransaction( |
| 84 | + session -> |
| 85 | + assertDoesNotThrow( |
| 86 | + () -> session.createNativeQuery( "select id, name from Person", Record.class) |
| 87 | + // map a Long onto a primitive long, shouldn't throw an exception |
| 88 | + .addScalar("id", Long.class) |
| 89 | + .addScalar("name", String.class) |
| 90 | + .getResultList() |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + static class Record { |
| 96 | + long id; |
| 97 | + String name; |
| 98 | + public Record(long id, String name) { |
| 99 | + this.id = id; |
| 100 | + this.name = name; |
| 101 | + } |
| 102 | + long id() { |
| 103 | + return id; |
| 104 | + } |
| 105 | + String name() { |
| 106 | + return name; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments