Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.

Commit 2a0c104

Browse files
committed
Automatic many-to-one determination in case of link to the identifiable object was implemented.
1 parent 745ec91 commit 2a0c104

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

jirm-orm/src/main/java/co/jirm/mapper/definition/SqlParameterDefinition.java

+49-8
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,26 @@ static Map<String, SqlParameterDefinition> getSqlBeanParameters(Class<?> k, SqlO
144144
"No SQL columns/parameters found for: {}", k);
145145
return parameters;
146146
}
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+
}
148167

149168
public Optional<SqlParameterObjectDefinition> getObjectDefinition() {
150169
return objectDefinition;
@@ -157,22 +176,44 @@ static SqlParameterDefinition parameterDef(
157176
Class<?> parameterType, int order) {
158177

159178
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;
165205
}
206+
}
166207

208+
if (manyToOneClass != null) {
167209
JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
168-
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
210+
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(manyToOneClass, config);
169211
checkState( ! od.getIdParameters().isEmpty(), "No id parameters");
170212
String sn = null;
171213
if (joinColumn != null)
172214
sn = joinColumn.name();
173215
if (sn == null)
174216
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
175-
FetchType fetch = manyToOne.fetch();
176217
int depth;
177218
if (FetchType.LAZY == fetch) {
178219
depth = 1;

0 commit comments

Comments
 (0)