AutoMappingUnknownColumnBehavior.java

  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. 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.  *
  25.  * @author Kazuki Shimizu
  26.  */
  27. public enum AutoMappingUnknownColumnBehavior {

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

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

  47.   /**
  48.    * Fail mapping. 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.    *
  59.    * @param mappedStatement
  60.    *          current mapped statement
  61.    * @param columnName
  62.    *          column name for mapping target
  63.    * @param propertyName
  64.    *          property name for mapping target
  65.    * @param propertyType
  66.    *          property type for mapping target (If this argument is not null, {@link org.apache.ibatis.type.TypeHandler}
  67.    *          for property type is not registered)
  68.    */
  69.   public abstract void doAction(MappedStatement mappedStatement, String columnName, String propertyName,
  70.       Class<?> propertyType);

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

  81.   private static class LogHolder {
  82.     private static final Log log = LogFactory.getLog(AutoMappingUnknownColumnBehavior.class);
  83.   }

  84. }