Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions jirm-orm/src/main/java/co/jirm/mapper/SqlObjectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ public class SqlObjectConfig {
private final SqlObjectConverter objectMapper;
private final transient Cache<Class<?>, SqlObjectDefinition<?>> cache;
private final int maximumLoadDepth = 4;

private final boolean autoManyToOneDetermination;

protected SqlObjectConfig(
NamingStrategy namingStrategy,
SqlObjectConverter objectMapper,
SqlParameterConverter converter,
Cache<Class<?>, SqlObjectDefinition<?>> cache) {
this(namingStrategy, objectMapper, converter, cache, false);
}

protected SqlObjectConfig(
NamingStrategy namingStrategy,
SqlObjectConverter objectMapper,
SqlParameterConverter converter,
Cache<Class<?>, SqlObjectDefinition<?>> cache) {
Cache<Class<?>, SqlObjectDefinition<?>> cache,
boolean autoManyToOneDetermination) {
super();
this.namingStrategy = namingStrategy;
this.converter = converter;
this.objectMapper = objectMapper;
this.cache = cache;
this.autoManyToOneDetermination = autoManyToOneDetermination;
}


Expand Down Expand Up @@ -72,6 +83,10 @@ public int getMaximumLoadDepth() {
public Cache<Class<?>, SqlObjectDefinition<?>> getCache() {
return cache;
}

public boolean doAutoDetermineManyToOne() {
return autoManyToOneDetermination;
}

public static SqlObjectConfig DEFAULT =
new SqlObjectConfig(DefaultNamingStrategy.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package co.jirm.mapper.definition;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;

Expand All @@ -36,6 +35,7 @@
import javax.persistence.Version;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;

import static co.jirm.core.util.JirmPrecondition.check;
Expand Down Expand Up @@ -145,7 +145,26 @@ static Map<String, SqlParameterDefinition> getSqlBeanParameters(Class<?> k, SqlO
"No SQL columns/parameters found for: {}", k);
return parameters;
}


static boolean isClassComplex(final Class<?> k) {
if (k.isAnnotationPresent(JsonIdentityInfo.class)) {
return true;
} else {
for (final Constructor<?> c : k.getDeclaredConstructors()) {
if (c.isAnnotationPresent(JsonCreator.class)) {
for (final Annotation[] as : c.getParameterAnnotations()) {
for (final Annotation a : as) {
if (JsonProperty.class.equals(a.annotationType())) {
return true;
}
}
}
}
}

return false;
}
}

public Optional<SqlParameterObjectDefinition> getObjectDefinition() {
return objectDefinition;
Expand All @@ -158,18 +177,44 @@ static SqlParameterDefinition parameterDef(
Class<?> parameterType, int order) {

final SqlParameterDefinition definition;
String sn = null;
ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
if (manyToOne != null) {
Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");

final Class<?> manyToOneClass; // if manyToOneClass == null ==> this parameter is simple; it's complex otherwise.
final FetchType fetch;
{
final ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
if (manyToOne != null) {
Class<?> subK = manyToOne.targetEntity();
// void.class is the default; the documentation forces usage of the the parameter type in the default case.
if (subK == null || subK.equals(void.class)) {
subK = parameterType;
}

manyToOneClass = subK;
fetch = manyToOne.fetch();
} else if (config.doAutoDetermineManyToOne() && isClassComplex(parameterType)) {
final SqlObjectDefinition<?> pd = SqlObjectDefinition.fromClass(parameterType, config);
if (!pd.getIdParameters().isEmpty()) {
manyToOneClass = parameterType;
fetch = FetchType.EAGER; // todo probably the fetch type should be configurable.
} else {
manyToOneClass = null;
fetch = null;
}
} else {
manyToOneClass = null;
fetch = null;
}
}

if (manyToOneClass != null) {
JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(manyToOneClass, config);
checkState( ! od.getIdParameters().isEmpty(), "No id parameters");
String sn = null;
if (joinColumn != null)
sn = joinColumn.name();
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
FetchType fetch = manyToOne.fetch();
int depth;
if (FetchType.LAZY == fetch) {
depth = 1;
Expand All @@ -182,6 +227,7 @@ static SqlParameterDefinition parameterDef(
definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
}
else {
String sn = null;
Column col = getAnnotation(objectType, parameterName, Column.class);
if (col != null && ! isNullOrEmpty(col.name()))
sn = col.name();
Expand Down