|
7 | 7 | package org.hibernate.orm.test.query.resultmapping;
|
8 | 8 |
|
9 | 9 | import org.hibernate.cfg.AvailableSettings;
|
| 10 | +import org.hibernate.dialect.H2Dialect; |
10 | 11 |
|
11 | 12 | import org.hibernate.testing.TestForIssue;
|
12 | 13 | import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
13 | 14 | import org.hibernate.testing.orm.junit.Jpa;
|
| 15 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
14 | 16 | import org.hibernate.testing.orm.junit.Setting;
|
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
15 | 19 | import org.junit.jupiter.api.Test;
|
16 | 20 |
|
17 | 21 | import jakarta.persistence.ColumnResult;
|
18 | 22 | import jakarta.persistence.Entity;
|
19 | 23 | import jakarta.persistence.Id;
|
20 | 24 | import jakarta.persistence.NamedNativeQuery;
|
21 | 25 | import jakarta.persistence.SqlResultSetMapping;
|
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static org.hamcrest.CoreMatchers.is; |
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; |
22 | 30 |
|
23 | 31 | /**
|
24 | 32 | * @author Nathan Xu
|
|
30 | 38 | @TestForIssue(jiraKey = "HHH-15070")
|
31 | 39 | class NamedNativeQueryWithCountColumnTest {
|
32 | 40 |
|
| 41 | + @BeforeEach |
| 42 | + public void setUp(EntityManagerFactoryScope scope) { |
| 43 | + scope.inTransaction( |
| 44 | + entityManager -> { |
| 45 | + for ( int i = 0; i < 3; i++ ) { |
| 46 | + Sample sample = new Sample( i, String.valueOf( i ) ); |
| 47 | + entityManager.persist( sample ); |
| 48 | + } |
| 49 | + } |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 55 | + scope.inTransaction( |
| 56 | + entityManager -> |
| 57 | + entityManager.createQuery( "delete from Sample" ).executeUpdate() |
| 58 | + ); |
| 59 | + } |
| 60 | + |
33 | 61 | @Test
|
34 |
| - void testNoNullPointerExceptionThrown(EntityManagerFactoryScope scope) { |
35 |
| - scope.inTransaction( em ->em.createNamedQuery( "sample.count", Long.class ) ); |
| 62 | + void testNamedNativeQuery(EntityManagerFactoryScope scope) { |
| 63 | + scope.inTransaction( |
| 64 | + entityManager -> |
| 65 | + entityManager.createNamedQuery( "sample.count", Long.class ) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @RequiresDialect(H2Dialect.class) |
| 71 | + void testNamedNativeQueryExecution(EntityManagerFactoryScope scope) { |
| 72 | + scope.inTransaction( |
| 73 | + entityManager -> { |
| 74 | + Long count = entityManager.createNamedQuery( "sample.count", Long.class ).getSingleResult(); |
| 75 | + assertThat( count, is( 3L ) ); |
| 76 | + } ); |
36 | 77 | }
|
37 | 78 |
|
38 | 79 | @SqlResultSetMapping(
|
39 |
| - name = "mapping", |
40 |
| - columns = @ColumnResult( name = "cnt" ) |
| 80 | + name = "mapping", |
| 81 | + columns = @ColumnResult(name = "cnt") |
41 | 82 | )
|
42 | 83 | @NamedNativeQuery(
|
43 |
| - name = "sample.count", |
44 |
| - resultSetMapping = "mapping", |
45 |
| - query = "SELECT count(*) AS cnt FROM Sample" |
| 84 | + name = "sample.count", |
| 85 | + resultSetMapping = "mapping", |
| 86 | + query = "SELECT count(*) AS cnt FROM SAMPLE_TABLE" |
46 | 87 | )
|
47 | 88 | @Entity(name = "Sample")
|
| 89 | + @Table(name = "SAMPLE_TABLE") |
48 | 90 | static class Sample {
|
49 | 91 |
|
50 | 92 | @Id
|
51 |
| - Long id; |
| 93 | + Integer id; |
| 94 | + |
| 95 | + String name; |
| 96 | + |
| 97 | + public Sample() { |
| 98 | + } |
52 | 99 |
|
| 100 | + public Sample(Integer id, String name) { |
| 101 | + this.id = id; |
| 102 | + this.name = name; |
| 103 | + } |
53 | 104 | }
|
54 | 105 | }
|
0 commit comments