From 0d2e39314f5fc901a8e5e639dbb9360af3723e32 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 5 Dec 2022 20:07:56 +0100 Subject: [PATCH] HHH-15653 Add test for issue --- .../RegisterNamedQueryWithParameterTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/RegisterNamedQueryWithParameterTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/RegisterNamedQueryWithParameterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/RegisterNamedQueryWithParameterTest.java new file mode 100644 index 0000000000..74421d66a5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/RegisterNamedQueryWithParameterTest.java @@ -0,0 +1,74 @@ +package org.hibernate.orm.test.jpa.query; + +import java.util.List; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.NamedNativeQuery; +import jakarta.persistence.Query; +import jakarta.persistence.Table; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = RegisterNamedQueryWithParameterTest.TestEntity.class +) +@TestForIssue(jiraKey = "HHH-15653") +public class RegisterNamedQueryWithParameterTest { + + private static final String QUERY_NAME = "ENTITY_BY_NAME"; + private static final String QUERY = "select t.id from TEST_ENTITY t where t.anInteger = :value"; + + @BeforeAll + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Query query = entityManager.createNativeQuery( QUERY ); + scope.getEntityManagerFactory().addNamedQuery( "ENTITY_BY_NAME", query ); + + TestEntity entity = new TestEntity( 1l, "And", 1 ); + TestEntity entity2 = new TestEntity( 2l, "Fab", 2 ); + entityManager.persist( entity ); + entityManager.persist( entity2 ); + } + ); + } + + @Test + public void testExecuteNativQuery(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Query query = entityManager.createNamedQuery( QUERY_NAME ); + query.setParameter( "value", 1 ); + List results = query.getResultList(); + assertThat( results.size() ).isEqualTo( 1 ); + } + ); + } + + @Entity(name = "TestEntity") + @Table(name = "TEST_ENTITY") + public static class TestEntity { + @Id + Long id; + + String name; + + Integer anInteger; + + public TestEntity() { + } + + public TestEntity(Long id, String name, Integer anInteger) { + this.id = id; + this.name = name; + this.anInteger = anInteger; + } + } +}