44 */
55package org .hibernate .orm .test .associations ;
66
7- import java .io .Serializable ;
8- import java .util .Objects ;
97import jakarta .persistence .Column ;
108import jakarta .persistence .Entity ;
119import jakarta .persistence .Id ;
1210import jakarta .persistence .JoinColumn ;
1311import jakarta .persistence .ManyToOne ;
1412import jakarta .persistence .Table ;
15-
1613import org .hibernate .annotations .JoinColumnOrFormula ;
1714import org .hibernate .annotations .JoinFormula ;
18- import org .hibernate .orm .test .jpa .BaseEntityManagerFunctionalTestCase ;
15+ import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
16+ import org .hibernate .testing .orm .junit .Jpa ;
17+ import org .junit .jupiter .api .Test ;
1918
20- import org .junit .Test ;
19+ import java .io .Serializable ;
20+ import java .util .Objects ;
2121
22- import static org .hibernate .testing .transaction .TransactionUtil .doInJPA ;
23- import static org .junit .Assert .assertEquals ;
22+ import static org .assertj .core .api .Assertions .assertThat ;
2423
2524/**
2625 * @author Vlad Mihalcea
2726 */
28- public class JoinColumnOrFormulaTest extends BaseEntityManagerFunctionalTestCase {
29-
30- @ Override
31- protected Class <?>[] getAnnotatedClasses () {
32- return new Class <?>[] {
33- Country .class ,
34- User .class
35- };
36- }
27+ @ Jpa (
28+ annotatedClasses = {
29+ JoinColumnOrFormulaTest .Country .class ,
30+ JoinColumnOrFormulaTest .User .class
31+ }
32+ )
33+ public class JoinColumnOrFormulaTest {
3734
3835 @ Test
39- public void testLifecycle () {
36+ public void testLifecycle (EntityManagerFactoryScope scope ) {
4037 //tag::associations-JoinColumnOrFormula-persistence-example[]
4138 Country US = new Country ();
42- US .setId (1 );
43- US .setDefault (true );
44- US .setPrimaryLanguage ("English" );
45- US .setName ("United States" );
39+ US .setId ( 1 );
40+ US .setDefault ( true );
41+ US .setPrimaryLanguage ( "English" );
42+ US .setName ( "United States" );
4643
4744 Country Romania = new Country ();
48- Romania .setId (40 );
49- Romania .setDefault (true );
50- Romania .setName ("Romania" );
51- Romania .setPrimaryLanguage ("Romanian" );
45+ Romania .setId ( 40 );
46+ Romania .setDefault ( true );
47+ Romania .setName ( "Romania" );
48+ Romania .setPrimaryLanguage ( "Romanian" );
5249
53- doInJPA ( this :: entityManagerFactory , entityManager -> {
54- entityManager .persist (US );
55- entityManager .persist (Romania );
56- });
50+ scope . inTransaction ( entityManager -> {
51+ entityManager .persist ( US );
52+ entityManager .persist ( Romania );
53+ } );
5754
58- doInJPA ( this :: entityManagerFactory , entityManager -> {
55+ scope . inTransaction ( entityManager -> {
5956 User user1 = new User ();
60- user1 .setId (1L );
61- user1 .setFirstName ("John" );
62- user1 .setLastName ("Doe" );
63- user1 .setLanguage ("English" );
64- entityManager .persist (user1 );
57+ user1 .setId ( 1L );
58+ user1 .setFirstName ( "John" );
59+ user1 .setLastName ( "Doe" );
60+ user1 .setLanguage ( "English" );
61+ entityManager .persist ( user1 );
6562
6663 User user2 = new User ();
67- user2 .setId (2L );
68- user2 .setFirstName ("Vlad" );
69- user2 .setLastName ("Mihalcea" );
70- user2 .setLanguage ("Romanian" );
71- entityManager .persist (user2 );
64+ user2 .setId ( 2L );
65+ user2 .setFirstName ( "Vlad" );
66+ user2 .setLastName ( "Mihalcea" );
67+ user2 .setLanguage ( "Romanian" );
68+ entityManager .persist ( user2 );
7269
73- });
70+ } );
7471 //end::associations-JoinColumnOrFormula-persistence-example[]
7572
7673 //tag::associations-JoinColumnOrFormula-fetching-example[]
77- doInJPA (this ::entityManagerFactory , entityManager -> {
78- log .info ("Fetch User entities" );
74+ scope .inTransaction ( entityManager -> {
75+ User john = entityManager .find ( User .class , 1L );
76+ assertThat ( john .getCountry () ).isEqualTo ( US );
7977
80- User john = entityManager .find (User .class , 1L );
81- assertEquals (US , john .getCountry ());
82-
83- User vlad = entityManager .find (User .class , 2L );
84- assertEquals (Romania , vlad .getCountry ());
85- });
78+ User vlad = entityManager .find ( User .class , 2L );
79+ assertThat ( vlad .getCountry () ).isEqualTo ( Romania );
80+ } );
8681 //end::associations-JoinColumnOrFormula-fetching-example[]
8782 }
8883
@@ -102,24 +97,24 @@ public static class User {
10297
10398 @ ManyToOne
10499 @ JoinColumnOrFormula (column =
105- @ JoinColumn (
100+ @ JoinColumn (
106101 name = "language" ,
107102 referencedColumnName = "primaryLanguage" ,
108103 insertable = false ,
109104 updatable = false
110- )
105+ )
111106 )
112107 @ JoinColumnOrFormula (formula =
113- @ JoinFormula (
108+ @ JoinFormula (
114109 value = "true" ,
115110 referencedColumnName = "is_default"
116- )
111+ )
117112 )
118113 private Country country ;
119114
120115 //Getters and setters omitted for brevity
121116
122- //end::associations-JoinColumnOrFormula-example[]
117+ //end::associations-JoinColumnOrFormula-example[]
123118 public Long getId () {
124119 return id ;
125120 }
@@ -160,7 +155,7 @@ public void setCountry(Country country) {
160155 this .country = country ;
161156 }
162157
163- //tag::associations-JoinColumnOrFormula-example[]
158+ //tag::associations-JoinColumnOrFormula-example[]
164159 }
165160 //end::associations-JoinColumnOrFormula-example[]
166161
@@ -182,7 +177,7 @@ public static class Country implements Serializable {
182177
183178 //Getters and setters, equals and hashCode methods omitted for brevity
184179
185- //end::associations-JoinColumnOrFormula-example[]
180+ //end::associations-JoinColumnOrFormula-example[]
186181
187182 public int getId () {
188183 return id ;
@@ -218,21 +213,21 @@ public void setDefault(boolean _default) {
218213
219214 @ Override
220215 public boolean equals (Object o ) {
221- if (this == o ) {
216+ if ( this == o ) {
222217 return true ;
223218 }
224- if (!(o instanceof Country )) {
219+ if ( !(o instanceof Country ) ) {
225220 return false ;
226221 }
227222 Country country = (Country ) o ;
228- return Objects .equals (getId (), country .getId ());
223+ return Objects .equals ( getId (), country .getId () );
229224 }
230225
231226 @ Override
232227 public int hashCode () {
233- return Objects .hash (getId ());
228+ return Objects .hash ( getId () );
234229 }
235- //tag::associations-JoinColumnOrFormula-example[]
230+ //tag::associations-JoinColumnOrFormula-example[]
236231 }
237232 //end::associations-JoinColumnOrFormula-example[]
238233}
0 commit comments