|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.List; |
9 | 9 |
|
10 | | -import org.hibernate.boot.SessionFactoryBuilder; |
11 | | - |
| 10 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
12 | 12 | import org.hibernate.testing.orm.junit.JiraKey; |
13 | | -import org.hibernate.testing.jdbc.SQLStatementInterceptor; |
14 | | -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
15 | | -import org.junit.Test; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
16 | 18 |
|
17 | 19 | import jakarta.persistence.CascadeType; |
18 | 20 | import jakarta.persistence.Column; |
|
23 | 25 | import jakarta.persistence.NamedQuery; |
24 | 26 | import jakarta.persistence.OneToMany; |
25 | 27 |
|
26 | | -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
27 | | -import static org.junit.Assert.assertEquals; |
28 | | -import static org.junit.Assert.assertTrue; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
29 | 30 |
|
30 | 31 | /** |
31 | 32 | * @author Vlad Mihalcea |
32 | 33 | */ |
33 | 34 | @JiraKey(value = "HHH-10965") |
34 | | -public class SelectDistinctHqlTest extends BaseNonConfigCoreFunctionalTestCase { |
| 35 | +@DomainModel(annotatedClasses = {SelectDistinctHqlTest.Person.class, SelectDistinctHqlTest.Phone.class}) |
| 36 | +@SessionFactory(useCollectingStatementInspector = true) |
| 37 | +public class SelectDistinctHqlTest { |
35 | 38 |
|
36 | 39 | private static final String DISTINCT_NAMED_QUERY = "distinct"; |
37 | | - private SQLStatementInterceptor sqlStatementInterceptor; |
38 | | - |
39 | | - @Override |
40 | | - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { |
41 | | - sqlStatementInterceptor = new SQLStatementInterceptor( sfb ); |
42 | | - } |
| 40 | + private SQLStatementInspector SQLStatementInspector; |
43 | 41 |
|
44 | | - @Override |
45 | | - protected Class<?>[] getAnnotatedClasses() { |
46 | | - return new Class<?>[] { |
47 | | - Person.class, |
48 | | - Phone.class, |
49 | | - }; |
50 | | - } |
51 | | - |
52 | | - protected void prepareTest() { |
53 | | - doInHibernate( this::sessionFactory, session -> { |
| 42 | + @BeforeEach |
| 43 | + protected void setup(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
54 | 45 | Person person = new Person(); |
55 | 46 | person.id = 1L; |
56 | 47 | session.persist( person ); |
57 | 48 |
|
58 | 49 | person.addPhone( new Phone( "027-123-4567" ) ); |
59 | 50 | person.addPhone( new Phone( "028-234-9876" ) ); |
60 | 51 | } ); |
| 52 | + |
| 53 | + SQLStatementInspector = scope.getCollectingStatementInspector(); |
| 54 | + SQLStatementInspector.clear(); |
61 | 55 | } |
62 | 56 |
|
63 | | - @Override |
64 | | - protected boolean isCleanupTestDataRequired() { |
65 | | - return true; |
| 57 | + @AfterEach |
| 58 | + protected void tearDown(SessionFactoryScope scope) { |
| 59 | + scope.dropData(); |
66 | 60 | } |
67 | 61 |
|
68 | 62 | @Test |
69 | | - public void test() { |
70 | | - doInHibernate( this::sessionFactory, session -> { |
71 | | - sqlStatementInterceptor.getSqlQueries().clear(); |
72 | | - List<Person> persons = session.createQuery( "select distinct p from Person p" ) |
| 63 | + public void test(SessionFactoryScope scope) { |
| 64 | + scope.inTransaction( session -> { |
| 65 | + SQLStatementInspector.getSqlQueries().clear(); |
| 66 | + List<Person> persons = session.createQuery( "select distinct p from Person p", Person.class ) |
73 | 67 | .getResultList(); |
74 | | - String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); |
| 68 | + String sqlQuery = SQLStatementInspector.getSqlQueries().get(0); |
75 | 69 | assertEquals( 1, persons.size() ); |
76 | 70 | assertTrue( sqlQuery.contains( " distinct " ) ); |
77 | 71 | } ); |
78 | 72 |
|
79 | | - doInHibernate( this::sessionFactory, session -> { |
80 | | - List<Person> persons = session.createQuery( "select p from Person p left join fetch p.phones " ) |
| 73 | + scope.inTransaction( session -> { |
| 74 | + List<Person> persons = session.createQuery( "select p from Person p left join fetch p.phones ", Person.class ) |
81 | 75 | .getResultList(); |
82 | 76 | // with Hibernate ORM 6 it is not necessary to use *distinct* to not duplicate the instances which own the association |
83 | 77 | assertEquals( 1, persons.size() ); |
84 | 78 | } ); |
85 | 79 |
|
86 | | - doInHibernate( this::sessionFactory, session -> { |
87 | | - sqlStatementInterceptor.getSqlQueries().clear(); |
88 | | - List<Person> persons = session.createQuery( "select distinct p from Person p left join fetch p.phones " ) |
| 80 | + scope.inTransaction( session -> { |
| 81 | + SQLStatementInspector.getSqlQueries().clear(); |
| 82 | + List<Person> persons = session.createQuery( "select distinct p from Person p left join fetch p.phones ", Person.class ) |
89 | 83 | .getResultList(); |
90 | 84 | assertEquals( 1, persons.size() ); |
91 | | - String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); |
| 85 | + String sqlQuery = SQLStatementInspector.getSqlQueries().get(0); |
92 | 86 | assertTrue( sqlQuery.contains( " distinct " ) ); |
93 | 87 | } ); |
94 | 88 | } |
95 | 89 |
|
96 | 90 | @Test |
97 | 91 | @JiraKey(value = "HHH-13780") |
98 | | - public void testNamedQueryDistinctPassThroughTrueWhenNotSpecified() { |
99 | | - doInHibernate( this::sessionFactory, session -> { |
100 | | - sqlStatementInterceptor.getSqlQueries().clear(); |
| 92 | + public void testNamedQueryDistinctPassThroughTrueWhenNotSpecified(SessionFactoryScope scope) { |
| 93 | + scope.inTransaction( session -> { |
| 94 | + SQLStatementInspector.getSqlQueries().clear(); |
101 | 95 | List<Person> persons = |
102 | 96 | session.createNamedQuery( DISTINCT_NAMED_QUERY, Person.class ) |
103 | 97 | .setMaxResults( 5 ) |
104 | 98 | .getResultList(); |
105 | 99 | assertEquals( 1, persons.size() ); |
106 | | - String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); |
| 100 | + String sqlQuery = SQLStatementInspector.getSqlQueries().get(0); |
107 | 101 | assertTrue( sqlQuery.contains( " distinct " ) ); |
108 | 102 | } ); |
109 | 103 | } |
|
0 commit comments