|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.query.resultmapping; |
| 8 | + |
| 9 | +import org.hibernate.cfg.AvailableSettings; |
| 10 | + |
| 11 | +import org.hibernate.testing.TestForIssue; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.hibernate.testing.orm.junit.Setting; |
| 15 | +import org.junit.jupiter.api.Assertions; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import jakarta.persistence.ColumnResult; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.NamedNativeQuery; |
| 23 | +import jakarta.persistence.SqlResultSetMapping; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Nathan Xu |
| 27 | + */ |
| 28 | +@Jpa( |
| 29 | + annotatedClasses = NamedNativeQueryTest.Sample.class, |
| 30 | + properties = @Setting(name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true") |
| 31 | +) |
| 32 | +@TestForIssue(jiraKey = "HHH-15070") |
| 33 | +class NamedNativeQueryTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void test(EntityManagerFactoryScope scope) { |
| 37 | + scope.inTransaction( em -> |
| 38 | + Assertions.assertDoesNotThrow( |
| 39 | + () -> em.createNamedQuery( "sample.count", Long.class ), |
| 40 | + "without fixing, NullPointerException would be thrown" |
| 41 | + ) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + @SqlResultSetMapping( |
| 46 | + name = "mapping", |
| 47 | + columns = @ColumnResult( name = "cnt" ) |
| 48 | + ) |
| 49 | + @NamedNativeQuery( |
| 50 | + name = "sample.count", |
| 51 | + resultSetMapping = "mapping", |
| 52 | + query = "SELECT count(*) AS cnt FROM Sample" |
| 53 | + ) |
| 54 | + @Entity(name = "Sample") |
| 55 | + static class Sample { |
| 56 | + |
| 57 | + @Id |
| 58 | + @GeneratedValue |
| 59 | + Long id; |
| 60 | + |
| 61 | + } |
| 62 | +} |
0 commit comments