AutoMappingUnknownColumnBehavior.java

  1. /*
  2.  *    Copyright 2009-2021 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. import org.apache.ibatis.logging.Log;
  18. import org.apache.ibatis.logging.LogFactory;
  19. import org.apache.ibatis.mapping.MappedStatement;

  20. /**
  21.  * Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
  22.  *
  23.  * @since 3.4.0
  24.  * @author Kazuki Shimizu
  25.  */
  26. public enum AutoMappingUnknownColumnBehavior {

  27.   /**
  28.    * Do nothing (Default).
  29.    */
  30.   NONE {
  31.     @Override
  32.     public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
  33.       // do nothing
  34.     }
  35.   },

  36.   /**
  37.    * Output warning log.
  38.    * Note: The log level of {@code 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior'} must be set to {@code WARN}.
  39.    */
  40.   WARNING {
  41.     @Override
  42.     public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
  43.       LogHolder.log.warn(buildMessage(mappedStatement, columnName, property, propertyType));
  44.     }
  45.   },

  46.   /**
  47.    * Fail mapping.
  48.    * Note: throw {@link SqlSessionException}.
  49.    */
  50.   FAILING {
  51.     @Override
  52.     public void doAction(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
  53.       throw new SqlSessionException(buildMessage(mappedStatement, columnName, property, propertyType));
  54.     }
  55.   };

  56.   /**
  57.    * Perform the action when detects an unknown column (or unknown property type) of automatic mapping target.
  58.    * @param mappedStatement current mapped statement
  59.    * @param columnName column name for mapping target
  60.    * @param propertyName property name for mapping target
  61.    * @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)
  62.      */
  63.   public abstract void doAction(MappedStatement mappedStatement, String columnName, String propertyName, Class<?> propertyType);

  64.   /**
  65.    * build error message.
  66.    */
  67.   private static String buildMessage(MappedStatement mappedStatement, String columnName, String property, Class<?> propertyType) {
  68.     return new StringBuilder("Unknown column is detected on '")
  69.       .append(mappedStatement.getId())
  70.       .append("' auto-mapping. Mapping parameters are ")
  71.       .append("[")
  72.       .append("columnName=").append(columnName)
  73.       .append(",").append("propertyName=").append(property)
  74.       .append(",").append("propertyType=").append(propertyType != null ? propertyType.getName() : null)
  75.       .append("]")
  76.       .toString();
  77.   }

  78.   private static class LogHolder {
  79.     private static final Log log = LogFactory.getLog(AutoMappingUnknownColumnBehavior.class);
  80.   }

  81. }