|
12 | 12 | import jakarta.persistence.criteria.Root; |
13 | 13 |
|
14 | 14 | import org.hibernate.Hibernate; |
15 | | -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; |
16 | 15 |
|
| 16 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
17 | 17 | import org.hibernate.testing.orm.junit.JiraKey; |
18 | | -import org.junit.Assert; |
19 | | -import org.junit.Test; |
| 18 | +import org.hibernate.testing.orm.junit.Jpa; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.Test; |
20 | 21 |
|
21 | | -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
22 | | -import static org.junit.Assert.assertTrue; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
23 | 24 |
|
24 | 25 | /** |
25 | 26 | * @author alan.oleary |
26 | 27 | */ |
27 | | -public class ComponentCriteriaTest extends BaseEntityManagerFunctionalTestCase { |
28 | | - @Override |
29 | | - public Class[] getAnnotatedClasses() { |
30 | | - return new Class[] { Client.class, Alias.class }; |
| 28 | +@Jpa(annotatedClasses = {Client.class, Alias.class}) |
| 29 | +public class ComponentCriteriaTest { |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 33 | + scope.getEntityManagerFactory().getSchemaManager().truncate(); |
31 | 34 | } |
32 | 35 |
|
33 | 36 | @Test |
34 | | - public void testEmbeddableInPath() { |
| 37 | + public void testEmbeddableInPath(EntityManagerFactoryScope scope) { |
35 | 38 | Client client = new Client( 111, "steve", "ebersole" ); |
36 | | - doInJPA( this::entityManagerFactory, em -> { |
37 | | - em.persist( client ); |
38 | | - } ); |
| 39 | + scope.inTransaction( entityManager -> entityManager.persist( client ) ); |
39 | 40 |
|
40 | | - doInJPA( this::entityManagerFactory, em -> { |
41 | | - CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 41 | + scope.inTransaction( entityManager -> { |
| 42 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
42 | 43 | CriteriaQuery<Client> cq = cb.createQuery( Client.class ); |
43 | 44 | Root<Client> root = cq.from( Client.class ); |
44 | 45 | cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) ); |
45 | | - List<Client> list = em.createQuery( cq ).getResultList(); |
46 | | - Assert.assertEquals( 1, list.size() ); |
| 46 | + List<Client> list = entityManager.createQuery( cq ).getResultList(); |
| 47 | + assertEquals( 1, list.size() ); |
47 | 48 | } ); |
48 | 49 |
|
49 | 50 | // HHH-5792 |
50 | | - doInJPA( this::entityManagerFactory, em -> { |
51 | | - TypedQuery<Client> q = em.createQuery( |
| 51 | + scope.inTransaction( entityManager -> { |
| 52 | + TypedQuery<Client> q = entityManager.createQuery( |
52 | 53 | "SELECT c FROM Client c JOIN c.name n WHERE n.firstName = '" |
53 | 54 | + client.getName().getFirstName() + "'", |
54 | 55 | Client.class |
55 | 56 | ); |
56 | | - Assert.assertEquals( 1, q.getResultList().size() ); |
57 | | - } ); |
58 | | - |
59 | | - doInJPA( this::entityManagerFactory, em -> { |
60 | | - em.createQuery( "delete Client" ).executeUpdate(); |
| 57 | + assertEquals( 1, q.getResultList().size() ); |
61 | 58 | } ); |
62 | 59 | } |
63 | 60 |
|
64 | 61 | @Test |
65 | 62 | @JiraKey(value = "HHH-9642") |
66 | | - public void testOneToManyJoinFetchedInEmbeddable() { |
| 63 | + public void testOneToManyJoinFetchedInEmbeddable(EntityManagerFactoryScope scope) { |
67 | 64 | Client client = new Client( 111, "steve", "ebersole" ); |
68 | 65 | Alias alias = new Alias( "a", "guy", "work" ); |
69 | 66 | client.getName().getAliases().add( alias ); |
70 | | - doInJPA( this::entityManagerFactory, em -> { |
71 | | - em.persist( client ); |
72 | | - } ); |
| 67 | + scope.inTransaction( entityManager -> entityManager.persist( client ) ); |
73 | 68 |
|
74 | 69 | List<Client> list = new ArrayList<>(); |
75 | | - doInJPA( this::entityManagerFactory, em -> { |
76 | | - CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 70 | + scope.inTransaction( entityManager -> { |
| 71 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
77 | 72 | CriteriaQuery<Client> cq = cb.createQuery( Client.class ); |
78 | 73 | Root<Client> root = cq.from( Client.class ); |
79 | 74 | root.fetch( Client_.name ).fetch( Name_.aliases ); |
80 | 75 | cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) ); |
81 | | - list.addAll( em.createQuery( cq ).getResultList() ); |
82 | | - Assert.assertEquals( 1, list.size() ); |
| 76 | + list.addAll( entityManager.createQuery( cq ).getResultList() ); |
| 77 | + assertEquals( 1, list.size() ); |
83 | 78 | Client c = list.get( 0 ); |
84 | 79 | assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); |
85 | 80 | } ); |
86 | 81 |
|
87 | | - doInJPA( this::entityManagerFactory, em -> { |
88 | | - TypedQuery<Client> q = em.createQuery( |
| 82 | + scope.inTransaction( entityManager -> { |
| 83 | + TypedQuery<Client> q = entityManager.createQuery( |
89 | 84 | "SELECT c FROM Client c JOIN FETCH c.name.aliases WHERE c.name.firstName = '" |
90 | 85 | + client.getName().getFirstName() + "'", |
91 | 86 | Client.class |
92 | 87 | ); |
93 | | - Assert.assertEquals( 1, q.getResultList().size() ); |
| 88 | + assertEquals( 1, q.getResultList().size() ); |
94 | 89 | Client c = list.get( 0 ); |
95 | 90 | assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); |
96 | 91 | } ); |
97 | 92 |
|
98 | | - doInJPA( this::entityManagerFactory, em -> { |
99 | | - TypedQuery<Client> q = em.createQuery( |
| 93 | + scope.inTransaction( entityManager -> { |
| 94 | + TypedQuery<Client> q = entityManager.createQuery( |
100 | 95 | "SELECT c FROM Client c JOIN c.name n join FETCH n.aliases WHERE c.name.firstName = '" |
101 | 96 | + client.getName().getFirstName() + "'", |
102 | 97 | Client.class |
103 | 98 | ); |
104 | | - Assert.assertEquals( 1, q.getResultList().size() ); |
| 99 | + assertEquals( 1, q.getResultList().size() ); |
105 | 100 | Client c = list.get( 0 ); |
106 | 101 | assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); |
107 | 102 | } ); |
108 | 103 |
|
109 | | - doInJPA( this::entityManagerFactory, em -> { |
110 | | - Client c = em.merge( client ); |
111 | | - em.remove( c ); |
| 104 | + scope.inTransaction( entityManager -> { |
| 105 | + Client c = entityManager.merge( client ); |
| 106 | + entityManager.remove( c ); |
112 | 107 | } ); |
113 | 108 | } |
114 | 109 |
|
115 | 110 | @Test |
116 | 111 | @JiraKey(value = "HHH-4586") |
117 | | - public void testParameterizedFunctions() { |
118 | | - doInJPA( this::entityManagerFactory, em -> { |
119 | | - CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 112 | + public void testParameterizedFunctions(EntityManagerFactoryScope scope) { |
| 113 | + scope.inTransaction( entityManager -> { |
| 114 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); |
120 | 115 | // lower |
121 | 116 | CriteriaQuery<Client> cq = cb.createQuery( Client.class ); |
122 | 117 | Root<Client> root = cq.from( Client.class ); |
123 | 118 | cq.where( cb.equal( cb.lower( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) ); |
124 | | - em.createQuery( cq ).getResultList(); |
| 119 | + entityManager.createQuery( cq ).getResultList(); |
125 | 120 | // upper |
126 | 121 | cq = cb.createQuery( Client.class ); |
127 | 122 | root = cq.from( Client.class ); |
128 | 123 | cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) ); |
129 | | - em.createQuery( cq ).getResultList(); |
| 124 | + entityManager.createQuery( cq ).getResultList(); |
130 | 125 | } |
131 | 126 | ); |
132 | 127 | } |
|
0 commit comments