test for @NamedNativeQuery with result set mapping

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-13 11:54:36 +02:00 committed by Steve Ebersole
parent e11ac26cd2
commit a82ac08d14
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package org.hibernate.orm.test.jpa.query;
import jakarta.persistence.ColumnResult;
import jakarta.persistence.ConstructorResult;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityResult;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.Table;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
@SessionFactory
@DomainModel(annotatedClasses = NamedNativeQueryWithResultMappingTest.Mapped.class)
public class NamedNativeQueryWithResultMappingTest {
@Test void test(SessionFactoryScope scope) {
Mapped mapped = new Mapped();
mapped.name = "Gavin";
scope.inTransaction(s -> s.persist(mapped));
scope.inSession(s -> {
s.createNamedSelectionQuery("mapped-native-query").getSingleResult();
s.createNamedSelectionQuery("unmapped-native-query").getSingleResult();
});
}
@NamedNativeQuery(
name = "mapped-native-query",
query = "select id, name, 1 as one, 'hello' as hello from mapped",
entities = @EntityResult(entityClass = Mapped.class),
columns = {@ColumnResult(name = "one", type = Integer.class),
@ColumnResult(name = "hello", type = String.class)}
)
@NamedNativeQuery(
name = "unmapped-native-query",
query = "select id, name, 1 as one from mapped",
classes = @ConstructorResult(targetClass = Unmapped.class,
columns = {@ColumnResult(name = "name"),
@ColumnResult(name = "id"),
@ColumnResult(name = "one")})
)
@Entity(name = "Mapped")
@Table(name = "mapped")
static class Mapped {
@Id @GeneratedValue
Long id;
String name;
}
record Unmapped(String name, long id, int one) {}
}