test for @NamedNativeQuery with result set mapping
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
e11ac26cd2
commit
a82ac08d14
|
@ -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) {}
|
||||||
|
}
|
Loading…
Reference in New Issue