|
12 | 12 | import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
13 | 13 | import org.hibernate.testing.orm.junit.Jpa;
|
14 | 14 | import org.hibernate.testing.orm.junit.Setting;
|
| 15 | +import org.junit.jupiter.api.BeforeEach; |
15 | 16 | import org.junit.jupiter.api.Test;
|
16 | 17 |
|
17 | 18 | import jakarta.persistence.ColumnResult;
|
18 | 19 | import jakarta.persistence.Entity;
|
19 | 20 | import jakarta.persistence.Id;
|
20 | 21 | import jakarta.persistence.NamedNativeQuery;
|
21 | 22 | import jakarta.persistence.SqlResultSetMapping;
|
| 23 | +import jakarta.persistence.Table; |
| 24 | + |
| 25 | +import static org.hamcrest.CoreMatchers.is; |
| 26 | +import static org.hamcrest.MatcherAssert.assertThat; |
22 | 27 |
|
23 | 28 | /**
|
24 | 29 | * @author Nathan Xu
|
|
30 | 35 | @TestForIssue(jiraKey = "HHH-15070")
|
31 | 36 | class NamedNativeQueryWithCountColumnTest {
|
32 | 37 |
|
| 38 | + @BeforeEach |
| 39 | + public void setUp(EntityManagerFactoryScope scope) { |
| 40 | + scope.inTransaction( |
| 41 | + entityManager -> { |
| 42 | + for ( int i = 0; i < 3; i++ ) { |
| 43 | + Sample sample = new Sample( i, String.valueOf( i ) ); |
| 44 | + entityManager.persist( sample ); |
| 45 | + } |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
33 | 50 | @Test
|
34 |
| - void testNoNullPointerExceptionThrown(EntityManagerFactoryScope scope) { |
35 |
| - scope.inTransaction( em ->em.createNamedQuery( "sample.count", Long.class ) ); |
| 51 | + void testNamedNativeQuery(EntityManagerFactoryScope scope) { |
| 52 | + scope.inTransaction( em -> { |
| 53 | + Long count = em.createNamedQuery( "sample.count", Long.class ).getSingleResult(); |
| 54 | + assertThat( count, is( 3l ) ); |
| 55 | + } ); |
36 | 56 | }
|
37 | 57 |
|
38 | 58 | @SqlResultSetMapping(
|
39 |
| - name = "mapping", |
40 |
| - columns = @ColumnResult( name = "cnt" ) |
| 59 | + name = "mapping", |
| 60 | + columns = @ColumnResult(name = "cnt") |
41 | 61 | )
|
42 | 62 | @NamedNativeQuery(
|
43 |
| - name = "sample.count", |
44 |
| - resultSetMapping = "mapping", |
45 |
| - query = "SELECT count(*) AS cnt FROM Sample" |
| 63 | + name = "sample.count", |
| 64 | + resultSetMapping = "mapping", |
| 65 | + query = "SELECT count(*) AS cnt FROM SAMPLE_TABLE" |
46 | 66 | )
|
47 | 67 | @Entity(name = "Sample")
|
| 68 | + @Table(name = "SAMPLE_TABLE") |
48 | 69 | static class Sample {
|
49 | 70 |
|
50 | 71 | @Id
|
51 |
| - Long id; |
| 72 | + Integer id; |
| 73 | + |
| 74 | + String name; |
| 75 | + |
| 76 | + public Sample() { |
| 77 | + } |
52 | 78 |
|
| 79 | + public Sample(Integer id, String name) { |
| 80 | + this.id = id; |
| 81 | + this.name = name; |
| 82 | + } |
53 | 83 | }
|
54 | 84 | }
|
0 commit comments