1 /**
2 * Copyright 2009-2018 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 * http://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 * @author Kazuki Shimizu
27 */
28 public enum AutoMappingUnknownColumnBehavior {
29
30 /**
31 * Do nothing (Default).
32 */
33 NONE {
34 @Override
35 public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
36 // do nothing
37 }
38 },
39
40 /**
41 * Output warning log.
42 * Note: The log level of {@code 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior'} must be set to {@code WARN}.
43 */
44 WARNING {
45 @Override
46 public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
47 LogHolder.log.warn(buildMessage(mappedStatement, columnName, property, propertyType));
48 }
49 },
50
51 /**
52 * Fail mapping.
53 * 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 * @param mappedStatement current mapped statement
65 * @param columnName column name for mapping target
66 * @param propertyName property name for mapping target
67 * @param propertyType property type for mapping target (If this argument is not null, {@link org.apache.ibatis.type.TypeHandler} for property type is not registered)
68 */
69 public abstract void doAction(MappedStatement mappedStatement, String columnName, String propertyName, Class<?> propertyType);
70
71 /**
72 * build error message.
73 */
74 private static String buildMessage(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
75 return new StringBuilder("Unknown column is detected on '")
76 .append(mappedStatement.getId())
77 .append("' auto-mapping. Mapping parameters are ")
78 .append("[")
79 .append("columnName=").append(columnName)
80 .append(",").append("propertyName=").append(property)
81 .append(",").append("propertyType=").append(propertyType != null ? propertyType.getName() : null)
82 .append("]")
83 .toString();
84 }
85
86 private static class LogHolder {
87 private static final Log log = LogFactory.getLog(AutoMappingUnknownColumnBehavior.class);
88 }
89
90 }