View Javadoc
1   /*
2    *    Copyright 2009-2023 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 org.apache.ibatis.logging.Log;
19  import org.apache.ibatis.logging.LogFactory;
20  import org.apache.ibatis.mapping.MappedStatement;
21  
22  /**
23   * Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
24   *
25   * @since 3.4.0
26   *
27   * @author Kazuki Shimizu
28   */
29  public enum AutoMappingUnknownColumnBehavior {
30  
31    /**
32     * Do nothing (Default).
33     */
34    NONE {
35      @Override
36      public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
37        // do nothing
38      }
39    },
40  
41    /**
42     * Output warning log. Note: The log level of {@code 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior'}
43     * must be set to {@code WARN}.
44     */
45    WARNING {
46      @Override
47      public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
48        LogHolder.log.warn(buildMessage(mappedStatement, columnName, property, propertyType));
49      }
50    },
51  
52    /**
53     * Fail mapping. Note: throw {@link SqlSessionException}.
54     */
55    FAILING {
56      @Override
57      public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
58        throw new SqlSessionException(buildMessage(mappedStatement, columnName, property, propertyType));
59      }
60    };
61  
62    /**
63     * Perform the action when detects an unknown column (or unknown property type) of automatic mapping target.
64     *
65     * @param mappedStatement
66     *          current mapped statement
67     * @param columnName
68     *          column name for mapping target
69     * @param propertyName
70     *          property name for mapping target
71     * @param propertyType
72     *          property type for mapping target (If this argument is not null, {@link org.apache.ibatis.type.TypeHandler}
73     *          for property type is not registered)
74     */
75    public abstract void doAction(MappedStatement mappedStatement, String columnName, String propertyName,
76        Class<?> propertyType);
77  
78    /**
79     * build error message.
80     */
81    private static String buildMessage(MappedStatement mappedStatement, String columnName, String property,
82        Class<?> propertyType) {
83      return new StringBuilder("Unknown column is detected on '").append(mappedStatement.getId())
84          .append("' auto-mapping. Mapping parameters are ").append("[").append("columnName=").append(columnName)
85          .append(",").append("propertyName=").append(property).append(",").append("propertyType=")
86          .append(propertyType != null ? propertyType.getName() : null).append("]").toString();
87    }
88  
89    private static class LogHolder {
90      private static final Log log = LogFactory.getLog(AutoMappingUnknownColumnBehavior.class);
91    }
92  
93  }