Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[demo-orm-jdbctemplate]BaseDao.getField()方法未正确过滤主键 #239

Open
t1anist opened this issue Oct 18, 2022 · 0 comments
Open
Assignees
Labels
bug 错误

Comments

@t1anist
Copy link

t1anist commented Oct 18, 2022

请先看《提问的智慧》,并尝试到 issue 列表 搜寻是否已经有人遇到过同样的问题。


描述问题

BaseDao.getField()方法未正确过滤主键

public static void main(String[] args) {
        BaseDao<User, Long> userDao = new UserDao(null);
        List<Field> field = userDao.getField(new User(), false);
        System.out.println(field);
    }

返回结果中包含了被注解@pk标为主键的id。原因如下:

private List<Field> getField(T t, Boolean ignoreNull) {
        // 获取所有字段,包含父类中的字段
        Field[] fields = ReflectUtil.getFields(t.getClass());

        // 过滤数据库中不存在的字段,以及自增列
        List<Field> filterField;
        Stream<Field> fieldStream = CollUtil.toList(fields).stream().filter(field -> ObjectUtil.isNull(field.getAnnotation(Ignore.class)) || ObjectUtil.isNull(field.getAnnotation(Pk.class)));

        // 是否过滤字段值为null的字段
        if (ignoreNull) {
            filterField = fieldStream.filter(field -> ObjectUtil.isNotNull(ReflectUtil.getFieldValue(t, field))).collect(Collectors.toList());
        } else {
            filterField = fieldStream.collect(Collectors.toList());
        }
        return filterField;
    }

此函数过滤fieldStream时,filter中的两个过滤条件应当是与关系,并非或关系

期待的结果

BaseDao.getField()方法正确过滤掉被@pk注解标为主键的Field

截屏或录像

使用||:
image
使用&&:
image

其他信息

请提供其他附加信息帮助我们诊断问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 错误
Projects
None yet
Development

No branches or pull requests

2 participants