|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.jpa.jointable; |
6 | 6 |
|
7 | | -import java.util.LinkedList; |
| 7 | +import java.util.List; |
8 | 8 |
|
9 | | -import org.hibernate.boot.SessionFactoryBuilder; |
10 | | -import org.hibernate.boot.spi.MetadataImplementor; |
11 | 9 | import org.hibernate.cfg.AvailableSettings; |
12 | | -import org.hibernate.engine.spi.SessionFactoryImplementor; |
13 | 10 |
|
14 | | -import org.hibernate.testing.jdbc.SQLStatementInterceptor; |
| 11 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
15 | 12 | import org.hibernate.testing.orm.junit.DomainModel; |
16 | 13 | import org.hibernate.testing.orm.junit.ServiceRegistry; |
17 | 14 | import org.hibernate.testing.orm.junit.SessionFactory; |
18 | | -import org.hibernate.testing.orm.junit.SessionFactoryProducer; |
19 | 15 | import org.hibernate.testing.orm.junit.SessionFactoryScope; |
20 | 16 | import org.hibernate.testing.orm.junit.Setting; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
21 | 18 | import org.junit.jupiter.api.Test; |
22 | 19 |
|
23 | 20 | import static org.hamcrest.CoreMatchers.is; |
24 | | -import static org.junit.Assert.assertFalse; |
25 | | -import static org.junit.Assert.assertThat; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
26 | 23 |
|
27 | 24 | /** |
28 | 25 | * @author Christian Beikov |
|
38 | 35 | @Setting(name = AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, value = "create-drop") |
39 | 36 | } |
40 | 37 | ) |
41 | | -@SessionFactory |
42 | | -public class ManyToOneJoinTableTest implements SessionFactoryProducer { |
43 | | - private SQLStatementInterceptor sqlStatementInterceptor; |
| 38 | +@SessionFactory(useCollectingStatementInspector = true) |
| 39 | +public class ManyToOneJoinTableTest { |
| 40 | + private SQLStatementInspector sqlStatementInspector; |
44 | 41 |
|
45 | | - @Override |
46 | | - public SessionFactoryImplementor produceSessionFactory(MetadataImplementor model) { |
47 | | - final SessionFactoryBuilder sessionFactoryBuilder = model.getSessionFactoryBuilder(); |
48 | | - sqlStatementInterceptor = new SQLStatementInterceptor( sessionFactoryBuilder ); |
49 | | - return (SessionFactoryImplementor) sessionFactoryBuilder.build(); |
| 42 | + @BeforeEach |
| 43 | + public void setup(SessionFactoryScope scope) { |
| 44 | + sqlStatementInspector = scope.getCollectingStatementInspector(); |
| 45 | + sqlStatementInspector.clear(); |
50 | 46 | } |
51 | 47 |
|
52 | 48 | @Test |
53 | 49 | public void testAvoidJoin(SessionFactoryScope scope) { |
54 | 50 | final String queryString = "SELECT e.id FROM Person e"; |
55 | 51 | scope.inTransaction( |
56 | 52 | session -> { |
57 | | - final LinkedList<String> sqlQueries = sqlStatementInterceptor.getSqlQueries(); |
| 53 | + final List<String> sqlQueries = sqlStatementInspector.getSqlQueries(); |
58 | 54 | sqlQueries.clear(); |
59 | 55 | session.createQuery( queryString ).list(); |
60 | 56 | assertThat( sqlQueries.size(), is( 1 ) ); |
61 | 57 | // Ideally, we could detect that *ToOne join tables aren't used, but that requires tracking the uses of properties |
62 | 58 | // Since *ToOne join tables are treated like secondary or subclass/superclass tables, the proper fix will allow many more optimizations |
63 | | - String generatedSQl = sqlQueries.getFirst(); |
| 59 | + String generatedSQl = sqlQueries.get( 0 ); |
64 | 60 | assertFalse( |
65 | | - "The generated sql contains a useless join: " + generatedSQl, |
66 | | - generatedSQl.contains( "join" ) |
| 61 | + generatedSQl.contains( "join" ), |
| 62 | + "The generated sql contains a useless join: " + generatedSQl |
67 | 63 | ); |
68 | 64 | } |
69 | 65 | ); |
|
0 commit comments