@@ -144,7 +144,26 @@ static Map<String, SqlParameterDefinition> getSqlBeanParameters(Class<?> k, SqlO
144
144
"No SQL columns/parameters found for: {}" , k );
145
145
return parameters ;
146
146
}
147
-
147
+
148
+ static boolean isClassComplex (final Class <?> k ) {
149
+ if (k .isAnnotationPresent (JsonIdentityInfo .class )) {
150
+ return true ;
151
+ } else {
152
+ for (final Constructor <?> c : k .getDeclaredConstructors ()) {
153
+ if (c .isAnnotationPresent (JsonCreator .class )) {
154
+ for (final Annotation [] as : c .getParameterAnnotations ()) {
155
+ for (final Annotation a : as ) {
156
+ if (JsonProperty .class .equals (a .annotationType ())) {
157
+ return true ;
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+
164
+ return false ;
165
+ }
166
+ }
148
167
149
168
public Optional <SqlParameterObjectDefinition > getObjectDefinition () {
150
169
return objectDefinition ;
@@ -157,22 +176,44 @@ static SqlParameterDefinition parameterDef(
157
176
Class <?> parameterType , int order ) {
158
177
159
178
final SqlParameterDefinition definition ;
160
- ManyToOne manyToOne = getAnnotation (objectType , parameterName , ManyToOne .class );
161
- if (manyToOne != null ) {
162
- Class <?> subK = manyToOne .targetEntity ();
163
- if (subK == null || subK .equals (void .class )) {
164
- subK = parameterType ;
179
+
180
+ final Class <?> manyToOneClass ; // if manyToOneClass == null ==> this parameter is simple; it's complex otherwise.
181
+ final FetchType fetch ;
182
+ {
183
+ final ManyToOne manyToOne = getAnnotation (objectType , parameterName , ManyToOne .class );
184
+ if (manyToOne != null ) {
185
+ Class <?> subK = manyToOne .targetEntity ();
186
+ // void.class is the default; the documentation forces usage of the the parameter type in the default case.
187
+ if (subK == null || subK .equals (void .class )) {
188
+ subK = parameterType ;
189
+ }
190
+
191
+ manyToOneClass = subK ;
192
+ fetch = manyToOne .fetch ();
193
+ } else if (config .doAutoDetermineManyToOne () && isClassComplex (parameterType )) {
194
+ final SqlObjectDefinition <?> pd = SqlObjectDefinition .fromClass (parameterType , config );
195
+ if (!pd .getIdParameters ().isEmpty ()) {
196
+ manyToOneClass = parameterType ;
197
+ fetch = FetchType .EAGER ; // todo probably the fetch type should be configurable.
198
+ } else {
199
+ manyToOneClass = null ;
200
+ fetch = null ;
201
+ }
202
+ } else {
203
+ manyToOneClass = null ;
204
+ fetch = null ;
165
205
}
206
+ }
166
207
208
+ if (manyToOneClass != null ) {
167
209
JoinColumn joinColumn = getAnnotation (objectType , parameterName , JoinColumn .class );
168
- SqlObjectDefinition <?> od = SqlObjectDefinition .fromClass (subK , config );
210
+ SqlObjectDefinition <?> od = SqlObjectDefinition .fromClass (manyToOneClass , config );
169
211
checkState ( ! od .getIdParameters ().isEmpty (), "No id parameters" );
170
212
String sn = null ;
171
213
if (joinColumn != null )
172
214
sn = joinColumn .name ();
173
215
if (sn == null )
174
216
sn = config .getNamingStrategy ().propertyToColumnName (parameterName );
175
- FetchType fetch = manyToOne .fetch ();
176
217
int depth ;
177
218
if (FetchType .LAZY == fetch ) {
178
219
depth = 1 ;
0 commit comments