|
| 1 | +package org.hibernate.orm.test.jpa.query; |
| 2 | + |
| 3 | +import jakarta.persistence.ColumnResult; |
| 4 | +import jakarta.persistence.ConstructorResult; |
| 5 | +import jakarta.persistence.Entity; |
| 6 | +import jakarta.persistence.EntityResult; |
| 7 | +import jakarta.persistence.GeneratedValue; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.NamedNativeQuery; |
| 10 | +import jakarta.persistence.Table; |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +@SessionFactory |
| 17 | +@DomainModel(annotatedClasses = NamedNativeQueryWithResultMappingTest.Mapped.class) |
| 18 | +public class NamedNativeQueryWithResultMappingTest { |
| 19 | + @Test void test(SessionFactoryScope scope) { |
| 20 | + Mapped mapped = new Mapped(); |
| 21 | + mapped.name = "Gavin"; |
| 22 | + scope.inTransaction(s -> s.persist(mapped)); |
| 23 | + scope.inSession(s -> { |
| 24 | + s.createNamedSelectionQuery("mapped-native-query").getSingleResult(); |
| 25 | + s.createNamedSelectionQuery("unmapped-native-query").getSingleResult(); |
| 26 | + }); |
| 27 | + } |
| 28 | + @NamedNativeQuery( |
| 29 | + name = "mapped-native-query", |
| 30 | + query = "select id, name, 1 as one, 'hello' as hello from mapped", |
| 31 | + entities = @EntityResult(entityClass = Mapped.class), |
| 32 | + columns = {@ColumnResult(name = "one", type = Integer.class), |
| 33 | + @ColumnResult(name = "hello", type = String.class)} |
| 34 | + ) |
| 35 | + @NamedNativeQuery( |
| 36 | + name = "unmapped-native-query", |
| 37 | + query = "select id, name, 1 as one from mapped", |
| 38 | + classes = @ConstructorResult(targetClass = Unmapped.class, |
| 39 | + columns = {@ColumnResult(name = "name"), |
| 40 | + @ColumnResult(name = "id"), |
| 41 | + @ColumnResult(name = "one")}) |
| 42 | + ) |
| 43 | + @Entity(name = "Mapped") |
| 44 | + @Table(name = "mapped") |
| 45 | + static class Mapped { |
| 46 | + @Id @GeneratedValue |
| 47 | + Long id; |
| 48 | + String name; |
| 49 | + } |
| 50 | + record Unmapped(String name, long id, int one) {} |
| 51 | +} |
0 commit comments