|
4 | 4 | */
|
5 | 5 | package org.hibernate.orm.test.query.criteria.internal.hhh13151;
|
6 | 6 |
|
7 |
| -import org.hibernate.Session; |
8 |
| -import org.hibernate.Transaction; |
9 |
| -import org.hibernate.cfg.AvailableSettings; |
10 |
| -import org.hibernate.cfg.Configuration; |
11 |
| -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
12 |
| -import org.junit.Before; |
13 |
| -import org.junit.Test; |
14 |
| - |
15 | 7 | import jakarta.persistence.criteria.CriteriaBuilder;
|
16 | 8 | import jakarta.persistence.criteria.CriteriaQuery;
|
17 | 9 | import jakarta.persistence.criteria.Root;
|
18 |
| - |
19 |
| -import java.util.List; |
20 |
| - |
21 |
| -import static junit.framework.TestCase.assertTrue; |
22 |
| - |
23 |
| -public class TreatedEntityFetchTest extends BaseCoreFunctionalTestCase { |
24 |
| - |
25 |
| - @Override |
26 |
| - protected Class<?>[] getAnnotatedClasses() { |
27 |
| - return new Class<?>[]{ |
28 |
| - SubEntity.class, |
29 |
| - SuperEntity.class, |
30 |
| - SideEntity.class |
31 |
| - }; |
32 |
| - } |
33 |
| - |
34 |
| - @Override |
35 |
| - protected void configure(Configuration configuration) { |
36 |
| - super.configure( configuration ); |
37 |
| - |
38 |
| - configuration.setProperty( AvailableSettings.SHOW_SQL, true ); |
39 |
| - configuration.setProperty( AvailableSettings.FORMAT_SQL, true ); |
40 |
| - // configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" ); |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +@DomainModel( annotatedClasses = {SubEntity.class, SuperEntity.class, SideEntity.class} ) |
| 21 | +@SessionFactory |
| 22 | +@JiraKey( "HHH-13151" ) |
| 23 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 24 | +public class TreatedEntityFetchTest { |
| 25 | + @Test |
| 26 | + public void testTreatedFetching(SessionFactoryScope sessions) throws Exception { |
| 27 | + final SubEntity result = (SubEntity) sessions.fromTransaction( (session) -> { |
| 28 | + final CriteriaBuilder cb = session.getCriteriaBuilder(); |
| 29 | + final CriteriaQuery<SuperEntity> criteria = cb.createQuery( SuperEntity.class ); |
| 30 | + final Root<SuperEntity> root = criteria.from( SuperEntity.class ); |
| 31 | + cb.treat( root, SubEntity.class ).fetch( "subField" ); |
| 32 | + |
| 33 | + return session.createQuery( criteria ).getResultList().get( 0 ); |
| 34 | + } ); |
| 35 | + |
| 36 | + final SideEntity subField = result.getSubField(); |
| 37 | + assertThat( subField.getName() ).isNotNull(); |
41 | 38 | }
|
42 | 39 |
|
43 |
| - @Before |
44 |
| - public void prepareEntities() { |
45 |
| - Session s = openSession(); |
46 |
| - Transaction tx = s.beginTransaction(); |
47 |
| - s.persist( new SubEntity().setSubField( new SideEntity( "testName" ) ) ); |
48 |
| - tx.commit(); |
49 |
| - s.close(); |
| 40 | + @BeforeEach |
| 41 | + public void createTestData(SessionFactoryScope sessions) { |
| 42 | + sessions.inTransaction( (session) -> { |
| 43 | + session.persist( new SubEntity().setSubField( new SideEntity( "testName" ) ) ); |
| 44 | + } ); |
50 | 45 | }
|
51 | 46 |
|
52 |
| - @Test |
53 |
| - public void hhh13151Test() throws Exception { |
54 |
| - Session s = openSession(); |
55 |
| - |
56 |
| - // Prepare Query |
57 |
| - CriteriaBuilder cb = s.getCriteriaBuilder(); |
58 |
| - CriteriaQuery<SuperEntity> criteria = cb.createQuery( SuperEntity.class ); |
59 |
| - Root<SuperEntity> root = criteria.from( SuperEntity.class ); |
60 |
| - cb.treat( root, SubEntity.class ).fetch( "subField" ); |
61 |
| - |
62 |
| - // Execute |
63 |
| - Transaction tx = s.beginTransaction(); |
64 |
| - List<SuperEntity> result = s.createQuery( criteria ).getResultList(); |
65 |
| - tx.commit(); |
66 |
| - s.close(); |
67 |
| - |
68 |
| - // Check results |
69 |
| - SideEntity subField = ( (SubEntity) result.get( 0 ) ).getSubField(); |
70 |
| - String name = subField.getName(); |
71 |
| - assertTrue( name != null ); |
| 47 | + @AfterEach |
| 48 | + public void dropTestData(SessionFactoryScope sessions) { |
| 49 | + sessions.dropData(); |
72 | 50 | }
|
73 | 51 | }
|
0 commit comments