|
| 1 | +package org.hibernate.orm.test.query; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.query.sql.spi.NativeQueryImplementor; |
| 6 | + |
| 7 | +import org.hibernate.testing.TestForIssue; |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import jakarta.persistence.Entity; |
| 16 | +import jakarta.persistence.Id; |
| 17 | +import jakarta.persistence.Tuple; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.is; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | + |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { NativeQuerySchemaPlaceholderTest.TestEntity.class } |
| 24 | +) |
| 25 | +@SessionFactory |
| 26 | +@TestForIssue(jiraKey = "HHH-15269") |
| 27 | +public class NativeQuerySchemaPlaceholderTest { |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + public void setUp(SessionFactoryScope scope) { |
| 31 | + scope.inTransaction( |
| 32 | + session -> { |
| 33 | + TestEntity testEntity = new TestEntity( 1l, "test" ); |
| 34 | + session.persist( testEntity ); |
| 35 | + } |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + public void tearDown(SessionFactoryScope scope) { |
| 41 | + scope.inTransaction( |
| 42 | + session -> { |
| 43 | + session.createMutationQuery( "delete from TestEntity" ).executeUpdate(); |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testUpdate(SessionFactoryScope scope) { |
| 50 | + scope.inTransaction( |
| 51 | + session -> { |
| 52 | + NativeQueryImplementor<Tuple> nativeQuery = session.createNativeQuery( |
| 53 | + "UPDATE {h-schema}TestEntity SET name = 'updated_test'" |
| 54 | + ); |
| 55 | + nativeQuery.executeUpdate(); |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + scope.inTransaction( |
| 60 | + session -> { |
| 61 | + List<TestEntity> testEntities = session.createQuery( "from TestEntity", TestEntity.class ) |
| 62 | + .list(); |
| 63 | + TestEntity testEntity = testEntities.get( 0 ); |
| 64 | + assertThat( testEntity.name, is( "updated_test" ) ); |
| 65 | + } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testSelect(SessionFactoryScope scope) { |
| 71 | + scope.inTransaction( |
| 72 | + session -> { |
| 73 | + NativeQueryImplementor<Long> nativeQuery = session.createNativeQuery( |
| 74 | + "select id from {h-schema}TestEntity", Long.class |
| 75 | + ); |
| 76 | + List<Long> results = nativeQuery.list(); |
| 77 | + assertThat( results.get( 0 ), is( 1l ) ); |
| 78 | + } |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Entity(name = "TestEntity") |
| 83 | + public static class TestEntity { |
| 84 | + @Id |
| 85 | + private Long id; |
| 86 | + |
| 87 | + private String name; |
| 88 | + |
| 89 | + public TestEntity() { |
| 90 | + } |
| 91 | + |
| 92 | + public TestEntity(Long id, String name) { |
| 93 | + this.id = id; |
| 94 | + this.name = name; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments