Mapper icon indicating copy to clipboard operation
Mapper copied to clipboard

父子实体私有同名属性或者get方法重写,当使用子实体类通用mapper, 会把父子同名属性都转成sql字段问题

Open xlxxcc opened this issue 8 years ago • 2 comments

父子实体私有同名属性或者get方法重写,当使用子实体类通用mapper, 会把父子同名属性都转成sql字段问题? 阅读代码后发现,Jdk8FieldHelper

static class Jdk8FieldHelper implements IFieldHelper {
        /**
         * 获取全部的Field
         *
         * @param entityClass
         * @return
         */
        public List<EntityField> getFields(Class<?> entityClass) {
            List<EntityField> fields = _getFields(entityClass, null, null);
            List<EntityField> properties = getProperties(entityClass);
            Set<EntityField> usedSet = new HashSet<EntityField>();
            for (EntityField field : fields) {
                for (EntityField property : properties) {              
                    if (!usedSet.contains(property) && field.getName().equals(property.getName())) {
                        //泛型的情况下通过属性可以得到实际的类型
                        field.setJavaType(property.getJavaType());
                        break;
                    }
                }
            }
            return fields;
        }

        /**
         * 获取全部的Field,仅仅通过Field获取
         *
         * @param entityClass
         * @param fieldList
         * @param level
         * @return
         */
        private List<EntityField> _getFields(Class<?> entityClass, List<EntityField> fieldList, Integer level) {
            if (fieldList == null) {
                fieldList = new ArrayList<EntityField>();
            }
            if (level == null) {
                level = 0;
            }
            if (entityClass.equals(Object.class)) {
                return fieldList;
            }
            Field[] fields = entityClass.getDeclaredFields();
            int index = 0;
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                //排除静态字段,解决bug#2
                if (!Modifier.isStatic(field.getModifiers())) {
                    if (level.intValue() != 0) {
                        //将父类的字段放在前面
                        fieldList.add(index, new EntityField(field, null));
                        index++;
                    } else {
                        fieldList.add(new EntityField(field, null));
                    }
                }
            }
            Class<?> superClass = entityClass.getSuperclass();
            if (superClass != null
                    && !superClass.equals(Object.class)
                    && (superClass.isAnnotationPresent(Entity.class)
                    || (!Map.class.isAssignableFrom(superClass)
                    && !Collection.class.isAssignableFrom(superClass)))) {
                return _getFields(entityClass.getSuperclass(), fieldList, ++level);
            }
            return fieldList;
        }

        /**
         * 通过方法获取属性
         *
         * @param entityClass
         * @return
         */
        public List<EntityField> getProperties(Class<?> entityClass) {
            List<EntityField> entityFields = new ArrayList<EntityField>();
            BeanInfo beanInfo = null;
            try {
                beanInfo = Introspector.getBeanInfo(entityClass);
            } catch (IntrospectionException e) {
                throw new RuntimeException(e);
            }
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor desc : descriptors) {
                if (!desc.getName().equals("class")) {
                    entityFields.add(new EntityField(null, desc));
                }
            }
            return entityFields;
        }
    }

针对getFields()方法,有个疑问: usedSet是空集合,为什么进行 if (!usedSet.contains(property) && field.getName().equals(property.getName())), 找不到 !usedSet.contains(property) 的意义。

针对_getFields()方法,会把父子实体的属性合并在一起, 且不会去掉父子同名属性。 因此在这里子类重写父类方法,是没有意义。 不知道是不是设计上的问题?

xlxxcc avatar Aug 21 '17 07:08 xlxxcc

确实不能覆盖,这里优先选择有配置的字段。

abel533 avatar Aug 22 '17 14:08 abel533

这个bug是不准备修复么我在继承的时候也是遇到这个问题如果子实体覆盖父类字段确实会有bug

nishubin avatar Nov 23 '21 02:11 nishubin