|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.hql.joinedSubclass; |
6 | 6 |
|
7 | | -import org.hibernate.cfg.AvailableSettings; |
8 | | -import org.hibernate.engine.spi.SessionFactoryImplementor; |
9 | | -import org.hibernate.type.SqlTypes; |
10 | | - |
11 | | -import org.hibernate.testing.orm.junit.JiraKey; |
12 | | -import org.hibernate.testing.orm.junit.DomainModel; |
13 | | -import org.hibernate.testing.orm.junit.SessionFactory; |
14 | | -import org.hibernate.testing.orm.junit.SessionFactoryScope; |
15 | | -import org.junit.jupiter.api.AfterAll; |
16 | | -import org.junit.jupiter.api.Assertions; |
17 | | -import org.junit.jupiter.api.BeforeAll; |
18 | | -import org.junit.jupiter.api.Test; |
19 | | - |
20 | 7 | import jakarta.persistence.Basic; |
21 | 8 | import jakarta.persistence.Column; |
22 | 9 | import jakarta.persistence.Entity; |
23 | 10 | import jakarta.persistence.GeneratedValue; |
24 | 11 | import jakarta.persistence.Id; |
25 | 12 | import jakarta.persistence.Inheritance; |
26 | 13 | import jakarta.persistence.InheritanceType; |
| 14 | +import org.hibernate.cfg.AvailableSettings; |
| 15 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 20 | +import org.hibernate.type.SqlTypes; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.Locale; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
27 | 28 |
|
28 | 29 | /** |
29 | 30 | * @author Jan Schatteman |
30 | 31 | */ |
31 | | -@DomainModel( |
32 | | - annotatedClasses = { |
33 | | - JoinedSubclassNativeQueryTest.Person.class, |
34 | | - JoinedSubclassNativeQueryTest.Employee.class |
35 | | - } |
36 | | -) |
| 32 | +@DomainModel( annotatedClasses = { |
| 33 | + JoinedSubclassNativeQueryTest.Person.class, |
| 34 | + JoinedSubclassNativeQueryTest.Employee.class |
| 35 | +} ) |
37 | 36 | @SessionFactory |
| 37 | +@SuppressWarnings("JUnitMalformedDeclaration") |
38 | 38 | public class JoinedSubclassNativeQueryTest { |
39 | 39 |
|
40 | | - @BeforeAll |
41 | | - public void setup(SessionFactoryScope scope) { |
42 | | - scope.inTransaction( |
43 | | - session -> { |
44 | | - Person p = new Person(); |
45 | | - p.setFirstName( "Jan" ); |
46 | | - session.persist( p ); |
47 | | - } |
| 40 | + @Test |
| 41 | + @JiraKey( value = "HHH-16180") |
| 42 | + public void testJoinedInheritanceNativeQuery(SessionFactoryScope scope) { |
| 43 | + final SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); |
| 44 | + final String nullColumnString = sessionFactory |
| 45 | + .getJdbcServices() |
| 46 | + .getDialect() |
| 47 | + .getSelectClauseNullString( SqlTypes.VARCHAR, sessionFactory.getTypeConfiguration() ); |
| 48 | + |
| 49 | + final String qry = String.format( |
| 50 | + Locale.ROOT, |
| 51 | + "select p.*, %s as company_name, 0 as clazz_ from Person p", |
| 52 | + nullColumnString |
48 | 53 | ); |
| 54 | + |
| 55 | + scope.inTransaction( (session) -> { |
| 56 | + // PostgreSQLDialect#getSelectClauseNullString produces e.g. `null::text` which we interpret as parameter, |
| 57 | + // so workaround this problem by configuring to ignore JDBC parameters |
| 58 | + session.setProperty( AvailableSettings.NATIVE_IGNORE_JDBC_PARAMETERS, true ); |
| 59 | + |
| 60 | + Person p = session.createNativeQuery( qry, Person.class ).getSingleResult(); |
| 61 | + assertThat( p ).isNotNull(); |
| 62 | + assertThat( p.getFirstName() ).isEqualTo( "Jan" ); |
| 63 | + } ); |
49 | 64 | } |
50 | 65 |
|
51 | | - @AfterAll |
52 | | - public void tearDown(SessionFactoryScope scope) { |
53 | | - scope.inTransaction( |
54 | | - session -> session.createMutationQuery( "delete from Person" ).executeUpdate() |
55 | | - ); |
| 66 | + @BeforeEach |
| 67 | + public void setup(SessionFactoryScope scope) { |
| 68 | + scope.inTransaction( (session) -> { |
| 69 | + Person p = new Person(); |
| 70 | + p.setFirstName( "Jan" ); |
| 71 | + session.persist( p ); |
| 72 | + } ); |
56 | 73 | } |
57 | 74 |
|
58 | | - @Test |
59 | | - @JiraKey( value = "HHH-16180") |
60 | | - public void testJoinedInheritanceNativeQuery(SessionFactoryScope scope) { |
61 | | - scope.inTransaction( |
62 | | - session -> { |
63 | | - final SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); |
64 | | - final String nullColumnString = sessionFactory |
65 | | - .getJdbcServices() |
66 | | - .getDialect() |
67 | | - .getSelectClauseNullString( SqlTypes.VARCHAR, sessionFactory.getTypeConfiguration() ); |
68 | | - // PostgreSQLDialect#getSelectClauseNullString produces e.g. `null::text` which we interpret as parameter, |
69 | | - // so workaround this problem by configuring to ignore JDBC parameters |
70 | | - session.setProperty( AvailableSettings.NATIVE_IGNORE_JDBC_PARAMETERS, true ); |
71 | | - Person p = (Person) session.createNativeQuery( "select p.*, " + nullColumnString + " as company_name, 0 as clazz_ from Person p", Person.class ).getSingleResult(); |
72 | | - Assertions.assertNotNull( p ); |
73 | | - Assertions.assertEquals( p.getFirstName(), "Jan" ); |
74 | | - } |
75 | | - ); |
| 75 | + @AfterEach |
| 76 | + public void tearDown(SessionFactoryScope scope) { |
| 77 | + scope.getSessionFactory().getSchemaManager().truncate(); |
76 | 78 | } |
77 | 79 |
|
78 | 80 | @Entity(name = "Person") |
|
0 commit comments