View Javadoc
1   /*
2    *    Copyright 2009-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.session;
17  
18  import java.lang.reflect.Type;
19  
20  import org.apache.ibatis.logging.Log;
21  import org.apache.ibatis.logging.LogFactory;
22  import org.apache.ibatis.mapping.MappedStatement;
23  
24  /**
25   * Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
26   *
27   * @since 3.4.0
28   *
29   * @author Kazuki Shimizu
30   */
31  public enum AutoMappingUnknownColumnBehavior {
32  
33    /**
34     * Do nothing (Default).
35     */
36    NONE {
37      @Override
38      public void doAction(MappedStatement mappedStatement, String columnName, String property, Type propertyType) {
39        // do nothing
40      }
41    },
42  
43    /**
44     * Output warning log. Note: The log level of {@code 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior'}
45     * must be set to {@code WARN}.
46     */
47    WARNING {
48      @Override
49      public void doAction(MappedStatement mappedStatement, String columnName, String property, Type propertyType) {
50        LogHolder.log.warn(buildMessage(mappedStatement, columnName, property, propertyType));
51      }
52    },
53  
54    /**
55     * Fail mapping. Note: throw {@link SqlSessionException}.
56     */
57    FAILING {
58      @Override
59      public void doAction(MappedStatement mappedStatement, String columnName, String property, Type propertyType) {
60        throw new SqlSessionException(buildMessage(mappedStatement, columnName, property, propertyType));
61      }
62    };
63  
64    /**
65     * Perform the action when detects an unknown column (or unknown property type) of automatic mapping target.
66     *
67     * @param mappedStatement
68     *          current mapped statement
69     * @param columnName
70     *          column name for mapping target
71     * @param propertyName
72     *          property name for mapping target
73     * @param propertyType
74     *          property type for mapping target (If this argument is not null, {@link org.apache.ibatis.type.TypeHandler}
75     *          for property type is not registered)
76     */
77    public abstract void doAction(MappedStatement mappedStatement, String columnName, String propertyName,
78        Type propertyType);
79  
80    /**
81     * build error message.
82     */
83    private static String buildMessage(MappedStatement mappedStatement, String columnName, String property,
84        Type propertyType) {
85      return new StringBuilder("Unknown column is detected on '").append(mappedStatement.getId())
86          .append("' auto-mapping. Mapping parameters are ").append("[").append("columnName=").append(columnName)
87          .append(",").append("propertyName=").append(property).append(",").append("propertyType=")
88          .append(propertyType != null ? propertyType.getTypeName() : null).append("]").toString();
89    }
90  
91    private static class LogHolder {
92      private static final Log log = LogFactory.getLog(AutoMappingUnknownColumnBehavior.class);
93    }
94  
95  }