Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,10 @@
*/
package org.apache.ibatis.builder;

import java.lang.reflect.Constructor;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -25,6 +27,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.cache.decorators.LruCache;
Expand Down Expand Up @@ -467,4 +470,66 @@ private Class<?> resolveParameterJavaType(Class<?> resultType, String property,
return javaType;
}

/**
* Attempts to assign a {@code javaType} to result mappings when it has been omitted, this is done based on matching
* constructors of the specified {@code resultType}
*
* @param resultType
* the result type of the object to be built
* @param resultMappings
* the current mappings
*
* @return null if there are no missing javaType mappings, or if no suitable mapping could be determined
*/
public List<ResultMapping> autoTypeResultMappingsForUnknownJavaTypes(Class<?> resultType,
List<ResultMapping> resultMappings) {
// check if we have any undefined java types present, and try to set them automatically
if (resultMappings.stream().noneMatch(resultMapping -> Object.class.equals(resultMapping.getJavaType()))) {
return null;
}

final List<List<Class<?>>> matchingConstructors = Arrays.stream(resultType.getDeclaredConstructors())
.map(Constructor::getParameterTypes).filter(parameters -> parameters.length == resultMappings.size())
.map(Arrays::asList).collect(Collectors.toList());

final List<Class<?>> typesToMatch = resultMappings.stream().map(ResultMapping::getJavaType)
.collect(Collectors.toList());

List<Class<?>> matchingTypes = null;

outer: for (final List<Class<?>> actualTypes : matchingConstructors) {
for (int j = 0; j < typesToMatch.size(); j++) {
final Class<?> type = typesToMatch.get(j);
// pre-filled a type, check if it matches the constructor
if (!Object.class.equals(type) && !type.equals(actualTypes.get(j))) {
continue outer;
}
}

if (matchingTypes != null) {
// multiple matches found, abort as we cannot reliably guess the correct one.
matchingTypes = null;
break;
}

matchingTypes = actualTypes;
}

if (matchingTypes == null) {
return null;
}

final List<ResultMapping> adjustedAutoTypeResultMappings = new ArrayList<>();
for (int i = 0; i < resultMappings.size(); i++) {
ResultMapping otherMapping = resultMappings.get(i);
Class<?> identifiedMatchingJavaType = matchingTypes.get(i);

// given that we selected a new java type, overwrite the currently
// selected type handler so it can get retrieved again from the registry
adjustedAutoTypeResultMappings
.add(new ResultMapping.Builder(otherMapping).javaType(identifiedMatchingJavaType).typeHandler(null).build());
}

return adjustedAutoTypeResultMappings;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -514,6 +515,7 @@ private boolean hasNestedSelect(Result result) {
}

private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMapping> resultMappings) {
final List<ResultMapping> mappings = new ArrayList<>();
for (Arg arg : args) {
List<ResultFlag> flags = new ArrayList<>();
flags.add(ResultFlag.CONSTRUCTOR);
Expand All @@ -527,8 +529,11 @@ private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMa
nullOrEmpty(arg.column()), arg.javaType() == void.class ? null : arg.javaType(),
arg.jdbcType() == JdbcType.UNDEFINED ? null : arg.jdbcType(), nullOrEmpty(arg.select()),
nullOrEmpty(arg.resultMap()), null, nullOrEmpty(arg.columnPrefix()), typeHandler, flags, null, null, false);
resultMappings.add(resultMapping);
mappings.add(resultMapping);
}

final List<ResultMapping> autoMappings = assistant.autoTypeResultMappingsForUnknownJavaTypes(resultType, mappings);
resultMappings.addAll(Objects.requireNonNullElse(autoMappings, mappings));
}

private String nullOrEmpty(String value) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import org.apache.ibatis.builder.BaseBuilder;
Expand Down Expand Up @@ -267,14 +268,21 @@ protected Class<?> inheritEnclosingType(XNode resultMapNode, Class<?> enclosingT

private void processConstructorElement(XNode resultChild, Class<?> resultType, List<ResultMapping> resultMappings) {
List<XNode> argChildren = resultChild.getChildren();

final List<ResultMapping> mappings = new ArrayList<>();
for (XNode argChild : argChildren) {
List<ResultFlag> flags = new ArrayList<>();
flags.add(ResultFlag.CONSTRUCTOR);
if ("idArg".equals(argChild.getName())) {
flags.add(ResultFlag.ID);
}
resultMappings.add(buildResultMappingFromContext(argChild, resultType, flags));

mappings.add(buildResultMappingFromContext(argChild, resultType, flags));
}

final List<ResultMapping> autoMappings = builderAssistant.autoTypeResultMappingsForUnknownJavaTypes(resultType,
mappings);
resultMappings.addAll(Objects.requireNonNullElse(autoMappings, mappings));
}

private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType,
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,13 @@
*/
package org.apache.ibatis.mapping;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.reflection.ParamNameUtil;
import org.apache.ibatis.session.Configuration;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
Expand All @@ -24,13 +31,6 @@
import java.util.Locale;
import java.util.Set;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.reflection.ParamNameUtil;
import org.apache.ibatis.session.Configuration;

/**
* @author Clinton Begin
*/
Expand Down Expand Up @@ -137,7 +137,7 @@ public ResultMap build() {
if (actualArgNames == null) {
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
+ ". Note that 'javaType' is required when there is no writable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
+ ". Note that 'javaType' is required when there is ambiguous constructors or there is no writable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
}
resultMap.constructorResultMappings.sort((o1, o2) -> {
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());
Expand Down
38 changes: 24 additions & 14 deletions src/main/java/org/apache/ibatis/mapping/ResultMapping.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,16 +15,16 @@
*/
package org.apache.ibatis.mapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* @author Clinton Begin
*/
Expand Down Expand Up @@ -72,6 +72,24 @@ public Builder(Configuration configuration, String property) {
resultMapping.lazy = configuration.isLazyLoadingEnabled();
}

public Builder(ResultMapping otherMapping) {
this(otherMapping.configuration, otherMapping.property);

resultMapping.flags.addAll(otherMapping.flags);
resultMapping.composites.addAll(otherMapping.composites);

resultMapping.column = otherMapping.column;
resultMapping.javaType = otherMapping.javaType;
resultMapping.jdbcType = otherMapping.jdbcType;
resultMapping.typeHandler = otherMapping.typeHandler;
resultMapping.nestedResultMapId = otherMapping.nestedResultMapId;
resultMapping.nestedQueryId = otherMapping.nestedQueryId;
resultMapping.notNullColumns = otherMapping.notNullColumns;
resultMapping.columnPrefix = otherMapping.columnPrefix;
resultMapping.resultSet = otherMapping.resultSet;
resultMapping.foreignColumn = otherMapping.foreignColumn;
}

public Builder javaType(Class<?> javaType) {
resultMapping.javaType = javaType;
return this;
Expand Down Expand Up @@ -243,18 +261,10 @@ public String getForeignColumn() {
return foreignColumn;
}

public void setForeignColumn(String foreignColumn) {
this.foreignColumn = foreignColumn;
}

public boolean isLazy() {
return lazy;
}

public void setLazy(boolean lazy) {
this.lazy = lazy;
}

public boolean isSimple() {
return this.nestedResultMapId == null && this.nestedQueryId == null && this.resultSet == null;
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
*/
package org.apache.ibatis.session;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.builder.CacheRefResolver;
import org.apache.ibatis.builder.IncompleteElementException;
Expand Down Expand Up @@ -83,20 +97,6 @@
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;

/**
* @author Clinton Begin
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.auto_type_from_non_ambiguous_constructor;

public record Account(long accountId, String accountName, String accountType) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.auto_type_from_non_ambiguous_constructor;

public record Account2(long accountId, String accountName, String accountType) {

public Account2(long accountId, String accountName, String accountType, String extraInfo) {
this(accountId, accountName, accountType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.auto_type_from_non_ambiguous_constructor;

public record Account3(long accountId, String accountName, String accountType) {

public Account3(long accountId, int mismatch, String accountType) {
this(accountId, "MismatchedAccountI", accountType);
}

public Account3(long accountId, long mismatch, String accountType) {
this(accountId, "MismatchedAccountL", accountType);
}
}
Loading
Loading