9
9
import java .util .List ;
10
10
11
11
import org .hibernate .LockOptions ;
12
- import org .hibernate .NotYetImplementedFor6Exception ;
12
+ import org .hibernate .engine . FetchTiming ;
13
13
import org .hibernate .engine .spi .LoadQueryInfluencers ;
14
14
import org .hibernate .engine .spi .SessionFactoryImplementor ;
15
15
import org .hibernate .loader .ast .internal .CollectionLoaderSingleKey ;
16
16
import org .hibernate .loader .ast .internal .SingleIdEntityLoaderStandardImpl ;
17
17
import org .hibernate .loader .ast .internal .SingleIdLoadPlan ;
18
+ import org .hibernate .loader .ast .spi .CascadingFetchProfile ;
18
19
import org .hibernate .metamodel .mapping .PluralAttributeMapping ;
19
20
import org .hibernate .persister .entity .EntityPersister ;
20
21
import org .hibernate .sql .results .graph .DomainResult ;
24
25
import org .hibernate .sql .results .graph .entity .EntityResult ;
25
26
26
27
import org .hibernate .testing .orm .junit .DomainModel ;
27
- import org .hibernate .testing .orm .junit .NotImplementedYet ;
28
28
import org .hibernate .testing .orm .junit .SessionFactory ;
29
29
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
30
30
import org .junit .jupiter .api .Test ;
31
31
32
32
import jakarta .persistence .CascadeType ;
33
33
import jakarta .persistence .Entity ;
34
+ import jakarta .persistence .FetchType ;
34
35
import jakarta .persistence .Id ;
35
36
import jakarta .persistence .JoinColumn ;
36
37
import jakarta .persistence .ManyToOne ;
37
38
import jakarta .persistence .OneToMany ;
38
39
39
- import static org .hamcrest .CoreMatchers .instanceOf ;
40
- import static org .hibernate .testing .hamcrest .CollectionMatchers .hasSize ;
41
- import static org .hibernate .testing .hamcrest .CollectionMatchers .isEmpty ;
42
- import static org .junit .Assert .assertThat ;
40
+ import static org .assertj .core .api .Assertions .assertThat ;
43
41
44
42
/**
45
43
* @author Steve Ebersole
@@ -54,62 +52,75 @@ public void testSimpleBuild(SessionFactoryScope scope) {
54
52
final SessionFactoryImplementor sessionFactory = scope .getSessionFactory ();
55
53
final EntityPersister entityDescriptor = sessionFactory .getDomainModel ().getEntityDescriptor ( Message .class );
56
54
57
- final SingleIdEntityLoaderStandardImpl loader = new SingleIdEntityLoaderStandardImpl (
58
- entityDescriptor ,
59
- sessionFactory
60
- );
55
+ final SingleIdEntityLoaderStandardImpl <?> loader = new SingleIdEntityLoaderStandardImpl <>( entityDescriptor , sessionFactory );
61
56
62
- final SingleIdLoadPlan loadPlan = loader .resolveLoadPlan (
57
+ final SingleIdLoadPlan <?> loadPlan = loader .resolveLoadPlan (
63
58
LockOptions .READ ,
64
59
LoadQueryInfluencers .NONE ,
65
60
sessionFactory
66
61
);
67
62
68
- assertThat (
69
- loadPlan .getJdbcSelect ()
70
- .getJdbcValuesMappingProducer ()
71
- .resolve ( null , sessionFactory )
72
- .getDomainResults (),
73
- hasSize ( 1 )
74
-
75
- );
76
- final DomainResult domainResult = loadPlan .getJdbcSelect ().getJdbcValuesMappingProducer ()
63
+ final List <DomainResult <?>> domainResults = loadPlan .getJdbcSelect ()
64
+ .getJdbcValuesMappingProducer ()
77
65
.resolve ( null , sessionFactory )
78
- .getDomainResults ()
79
- .get ( 0 );
80
- assertThat ( domainResult , instanceOf ( EntityResult .class ) );
66
+ .getDomainResults ();
67
+
68
+ assertThat ( domainResults ).hasSize ( 1 );
69
+ final DomainResult <?> domainResult = domainResults .get ( 0 );
70
+ assertThat ( domainResult ).isInstanceOf ( EntityResult .class );
81
71
final EntityResult entityResult = (EntityResult ) domainResult ;
82
- assertThat ( entityResult .getFetches (), hasSize ( 2 ) );
72
+ assertThat ( entityResult .getFetches () ). hasSize ( 2 );
83
73
84
74
final Fetch txtFetch = entityResult .getFetches ().get ( 0 );
75
+ assertThat ( txtFetch ).isNotNull ();
76
+ assertThat ( txtFetch .getFetchedMapping ().getFetchableName () ).isEqualTo ( "msgTxt" );
77
+ assertThat ( txtFetch .getTiming () ).isEqualTo ( FetchTiming .IMMEDIATE );
85
78
86
79
final Fetch posterFetch = entityResult .getFetches ().get ( 1 );
80
+ assertThat ( posterFetch ).isNotNull ();
81
+ assertThat ( posterFetch .getFetchedMapping ().getFetchableName () ).isEqualTo ( "poster" );
82
+ assertThat ( posterFetch .getTiming () ).isEqualTo ( FetchTiming .DELAYED );
87
83
}
88
84
89
85
@ Test
90
- @ NotImplementedYet (reason = "Cascade-driven DomainResult graph building not yet implemented" )
91
- public void testCascadeBasedBuild () {
92
- throw new NotYetImplementedFor6Exception ( "Cascade-driven DomainResult graph building not yet implemented" );
93
- // EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
94
- // CascadeStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new CascadeStyleLoadPlanBuildingAssociationVisitationStrategy(
95
- // CascadingActions.MERGE,
96
- // sessionFactory(),
97
- // LoadQueryInfluencers.NONE,
98
- // LockMode.NONE
99
- // );
100
- // LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan( strategy, ep );
101
- // assertFalse( plan.hasAnyScalarReturns() );
102
- // assertEquals( 1, plan.getReturns().size() );
103
- // Return rtn = plan.getReturns().get( 0 );
104
- // EntityReturn entityReturn = ExtraAssertions.assertTyping( EntityReturn.class, rtn );
105
- // assertNotNull( entityReturn.getFetches() );
106
- // assertEquals( 1, entityReturn.getFetches().length );
107
- // Fetch fetch = entityReturn.getFetches()[0];
108
- // EntityFetch entityFetch = ExtraAssertions.assertTyping( EntityFetch.class, fetch );
109
- // assertNotNull( entityFetch.getFetches() );
110
- // assertEquals( 0, entityFetch.getFetches().length );
111
- //
112
- // LoadPlanTreePrinter.INSTANCE.logTree( plan, new AliasResolutionContextImpl( sessionFactory() ) );
86
+ public void testCascadeBasedBuild (SessionFactoryScope scope ) {
87
+ final SessionFactoryImplementor sessionFactory = scope .getSessionFactory ();
88
+ final EntityPersister entityDescriptor = (EntityPersister ) sessionFactory .getRuntimeMetamodels ().getEntityMappingType ( Message .class );
89
+
90
+ final SingleIdEntityLoaderStandardImpl <?> loader = new SingleIdEntityLoaderStandardImpl <>( entityDescriptor , sessionFactory );
91
+
92
+ final LoadQueryInfluencers influencers = new LoadQueryInfluencers () {
93
+ @ Override
94
+ public CascadingFetchProfile getEnabledCascadingFetchProfile () {
95
+ return CascadingFetchProfile .MERGE ;
96
+ }
97
+ };
98
+
99
+ final SingleIdLoadPlan <?> loadPlan = loader .resolveLoadPlan (
100
+ LockOptions .READ ,
101
+ influencers ,
102
+ sessionFactory
103
+ );
104
+ final List <DomainResult <?>> domainResults = loadPlan .getJdbcSelect ()
105
+ .getJdbcValuesMappingProducer ()
106
+ .resolve ( null , sessionFactory )
107
+ .getDomainResults ();
108
+
109
+ assertThat ( domainResults ).hasSize ( 1 );
110
+ final DomainResult <?> domainResult = domainResults .get ( 0 );
111
+ assertThat ( domainResult ).isInstanceOf ( EntityResult .class );
112
+ final EntityResult entityResult = (EntityResult ) domainResult ;
113
+ assertThat ( entityResult .getFetches () ).hasSize ( 2 );
114
+
115
+ final Fetch txtFetch = entityResult .getFetches ().get ( 0 );
116
+ assertThat ( txtFetch ).isNotNull ();
117
+ assertThat ( txtFetch .getFetchedMapping ().getFetchableName () ).isEqualTo ( "msgTxt" );
118
+ assertThat ( txtFetch .getTiming () ).isEqualTo ( FetchTiming .IMMEDIATE );
119
+
120
+ final Fetch posterFetch = entityResult .getFetches ().get ( 1 );
121
+ assertThat ( posterFetch ).isNotNull ();
122
+ assertThat ( posterFetch .getFetchedMapping ().getFetchableName () ).isEqualTo ( "poster" );
123
+ assertThat ( posterFetch .getTiming () ).isEqualTo ( FetchTiming .IMMEDIATE );
113
124
}
114
125
115
126
@ Test
@@ -124,23 +135,23 @@ public void testCollectionInitializerCase(SessionFactoryScope scope) {
124
135
sessionFactory
125
136
);
126
137
127
- assertThat ( loader .getSqlAst ().getDomainResultDescriptors (), hasSize ( 1 ) );
128
- assertThat ( loader .getSqlAst ().getDomainResultDescriptors ().get ( 0 ), instanceOf ( CollectionDomainResult .class ) );
138
+ assertThat ( loader .getSqlAst ().getDomainResultDescriptors () ). hasSize ( 1 );
139
+ assertThat ( loader .getSqlAst ().getDomainResultDescriptors ().get ( 0 ) ). isInstanceOf ( CollectionDomainResult .class );
129
140
final CollectionDomainResult domainResult = (CollectionDomainResult ) loader .getSqlAst ()
130
141
.getDomainResultDescriptors ()
131
142
.get ( 0 );
132
143
133
144
DomainResultGraphPrinter .logDomainResultGraph ( loader .getSqlAst ().getDomainResultDescriptors () );
134
145
135
- assertThat ( domainResult .getFetches (), isEmpty () );
146
+ assertThat ( domainResult .getFetches () ). isEmpty ();
136
147
}
137
148
138
149
@ Entity ( name = "Message" )
139
150
public static class Message {
140
151
@ Id
141
152
private Integer mid ;
142
153
private String msgTxt ;
143
- @ ManyToOne ( cascade = CascadeType .MERGE )
154
+ @ ManyToOne ( fetch = FetchType . LAZY , cascade = CascadeType .MERGE )
144
155
@ JoinColumn
145
156
private Poster poster ;
146
157
}
0 commit comments