44 */
55package org .hibernate .orm .test .compositeelement ;
66
7- import org .hibernate .Session ;
8- import org .hibernate .Transaction ;
9- import org .hibernate .boot .Metadata ;
10- import org .hibernate .mapping .Collection ;
11- import org .hibernate .mapping .Component ;
12- import org .hibernate .mapping .Formula ;
13-
14- import org .hibernate .testing .junit4 .BaseNonConfigCoreFunctionalTestCase ;
15- import org .junit .Test ;
16-
177import jakarta .persistence .criteria .CriteriaBuilder ;
188import jakarta .persistence .criteria .CriteriaQuery ;
199import jakarta .persistence .criteria .Root ;
10+ import org .hibernate .testing .orm .junit .DomainModel ;
11+ import org .hibernate .testing .orm .junit .SessionFactory ;
12+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
13+ import org .junit .jupiter .api .AfterEach ;
14+ import org .junit .jupiter .api .Test ;
2015
21- import static org .junit . Assert . assertEquals ;
16+ import static org .assertj . core . api . Assertions . assertThat ;
2217
2318/**
2419 * @author Gavin King
2520 */
26- public class CompositeElementTest extends BaseNonConfigCoreFunctionalTestCase {
27-
28- @ Override
29- protected String getBaseForMappings () {
30- return "" ;
31- }
32-
33- @ Override
34- public String [] getMappings () {
35- return new String [] { "org/hibernate/orm/test/compositeelement/Parent.hbm.xml" };
36- }
37-
38- @ Override
39- protected void afterMetadataBuilt (Metadata metadata ) {
40- Collection children = metadata .getCollectionBinding ( Parent .class .getName () + ".children" );
41- Component childComponents = ( Component ) children .getElement ();
42- Formula f = ( Formula ) childComponents .getProperty ( "bioLength" ).getValue ().getSelectables ().get ( 0 );
43-
44- // SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get( "length" );
45- // if ( lengthFunction != null ) {
46- // ArrayList args = new ArrayList();
47- // args.add( "bio" );
48- // f.setFormula( lengthFunction.render( StandardBasicTypes.INTEGER, args, null ) );
49- // }
21+ @ DomainModel (
22+ xmlMappings = "org/hibernate/orm/test/compositeelement/Parent.hbm.xml"
23+ )
24+ @ SessionFactory
25+ public class CompositeElementTest {
26+
27+ @ AfterEach
28+ public void tearDown (SessionFactoryScope scope ) {
29+ scope .getSessionFactory ().getSchemaManager ().truncateMappedObjects ();
5030 }
5131
5232 @ Test
53- public void testHandSQL () {
54- Session s = openSession ();
55- Transaction t = s .beginTransaction ();
56- Child c = new Child ( "Child One" );
57- Parent p = new Parent ( "Parent" );
58- p .getChildren ().add ( c );
59- c .setParent ( p );
60- s .persist ( p );
61- s .flush ();
62-
63- p .getChildren ().remove ( c );
64- c .setParent ( null );
65- s .flush ();
66-
67- p .getChildren ().add ( c );
68- c .setParent ( p );
69- t .commit ();
70- s .close ();
71-
72- s = openSession ();
73- t = s .beginTransaction ();
74- s .createQuery ( "select distinct p from Parent p join p.children c where c.name like 'Child%'" ).uniqueResult ();
75- s .clear ();
76- s .createQuery ( "select new Child(c.name) from Parent p left outer join p.children c where c.name like 'Child%'" )
77- .uniqueResult ();
78- s .clear ();
79- //s.createQuery("select c from Parent p left outer join p.children c where c.name like 'Child%'").uniqueResult(); //we really need to be able to do this!
80- s .clear ();
81- p = ( Parent ) s .createQuery ( "from Parent p left join fetch p.children" ).uniqueResult ();
82- t .commit ();
83- s .close ();
84-
85- s = openSession ();
86- t = s .beginTransaction ();
87- s .remove ( p );
88- t .commit ();
89- s .close ();
33+ public void testHandSQL (SessionFactoryScope scope ) {
34+ scope .inTransaction (
35+ session -> {
36+ Child c = new Child ( "Child One" );
37+ Parent p = new Parent ( "Parent" );
38+ p .getChildren ().add ( c );
39+ c .setParent ( p );
40+ session .persist ( p );
41+ session .flush ();
42+
43+ p .getChildren ().remove ( c );
44+ c .setParent ( null );
45+ session .flush ();
46+
47+ p .getChildren ().add ( c );
48+ c .setParent ( p );
49+ }
50+ );
51+
52+ Parent parent = scope .fromTransaction ( session -> {
53+ session .createQuery ( "select distinct p from Parent p join p.children c where c.name like 'Child%'" ,
54+ Parent .class )
55+ .uniqueResult ();
56+ session .clear ();
57+ session .createQuery (
58+ "select new Child(c.name) from Parent p left outer join p.children c where c.name like 'Child%'" ,
59+ Child .class )
60+ .uniqueResult ();
61+ session .clear ();
62+ //s.createQuery("select c from Parent p left outer join p.children c where c.name like 'Child%'").uniqueResult(); //we really need to be able to do this!
63+ session .clear ();
64+ return session .createQuery ( "from Parent p left join fetch p.children" , Parent .class )
65+ .uniqueResult ();
66+ }
67+ );
68+
69+ scope .inTransaction (
70+ session ->
71+ session .remove ( parent )
72+ );
9073 }
9174
9275 @ Test
93- public void testCustomColumnReadAndWrite () {
94- inTransaction ( s -> {
76+ public void testCustomColumnReadAndWrite (SessionFactoryScope scope ) {
77+ scope . inTransaction ( s -> {
9578 Child c = new Child ( "Child One" );
9679 c .setPosition ( 1 );
9780 Parent p = new Parent ( "Parent" );
@@ -102,38 +85,38 @@ public void testCustomColumnReadAndWrite() {
10285
10386 // Oracle returns BigDecimaal while other dialects return Integer;
10487 // casting to Number so it works on all dialects
105- Number sqlValue = ( (Number ) s .createNativeQuery (
106- "select child_position from ParentChild c where c.name='Child One'" )
107- .uniqueResult () );
108- assertEquals ( 0 , sqlValue .intValue () );
88+ Number sqlValue = ((Number ) s .createNativeQuery (
89+ "select child_position from ParentChild c where c.name='Child One'" )
90+ .uniqueResult ());
91+ assertThat ( sqlValue .intValue () ). isEqualTo ( 0 );
10992
110- Integer hqlValue = ( Integer ) s .createQuery (
111- "select c.position from Parent p join p.children c where p.name='Parent'" )
93+ Integer hqlValue = s .createQuery (
94+ "select c.position from Parent p join p.children c where p.name='Parent'" , Integer . class )
11295 .uniqueResult ();
113- assertEquals ( 1 , hqlValue . intValue () );
96+ assertThat ( hqlValue ). isEqualTo ( 1 );
11497
11598// p = (Parent) s.createCriteria( Parent.class ).add( Restrictions.eq( "name", "Parent" ) ).uniqueResult();
11699
117100 CriteriaBuilder criteriaBuilder = s .getCriteriaBuilder ();
118101 CriteriaQuery <Parent > criteria = criteriaBuilder .createQuery ( Parent .class );
119102 Root <Parent > root = criteria .from ( Parent .class );
120- criteria .where ( criteriaBuilder .equal ( root .get ( "name" ),"Parent" ) );
103+ criteria .where ( criteriaBuilder .equal ( root .get ( "name" ), "Parent" ) );
121104
122105 p = s .createQuery ( criteria ).uniqueResult ();
123106
124- c = ( Child ) p .getChildren ().iterator ().next ();
125- assertEquals ( 1 , c .getPosition () );
107+ c = p .getChildren ().iterator ().next ();
108+ assertThat ( c .getPosition () ). isEqualTo ( 1 );
126109
127110 p = s .createQuery ( "from Parent p join p.children c where c.position = 1" , Parent .class ).uniqueResult ();
128- c = ( Child ) p .getChildren ().iterator ().next ();
129- assertEquals ( 1 , c .getPosition () );
111+ c = p .getChildren ().iterator ().next ();
112+ assertThat ( c .getPosition () ). isEqualTo ( 1 );
130113
131114 c .setPosition ( 2 );
132115 s .flush ();
133- sqlValue = ( (Number ) s .createNativeQuery (
134- "select child_position from ParentChild c where c.name='Child One'" )
135- .uniqueResult () );
136- assertEquals ( 1 , sqlValue .intValue () );
116+ sqlValue = ((Number ) s .createNativeQuery (
117+ "select child_position from ParentChild c where c.name='Child One'" )
118+ .uniqueResult ());
119+ assertThat ( sqlValue .intValue () ). isEqualTo ( 1 );
137120 s .remove ( p );
138121 } );
139122 }
0 commit comments