717 Star 2.8K Fork 762

abel533 / Mapper

 / 详情

updateByExample当有乐观锁时,会出现BUG

已完成
创建于  
2018-11-28 13:24

因为生成的SQL中,属性值需要拼上entityName,但是version字段没有拼就会报错,具体看下面代码:

public static String updateSetColumns(Class<?> entityClass, String entityName, boolean notNull, boolean notEmpty) {
        StringBuilder sql = new StringBuilder();
        sql.append("<set>");
        //获取全部列
        Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
        //对乐观锁的支持
        EntityColumn versionColumn = null;
        //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnSet) {
            if (column.getEntityField().isAnnotationPresent(Version.class)) {
                if (versionColumn != null) {
                    throw new VersionException(entityClass.getCanonicalName() + " 中包含多个带有 @Version 注解的字段,一个类中只能存在一个带有 @Version 注解的字段!");
                }
                versionColumn = column;
            }
            if (!column.isId() && column.isUpdatable()) {
                if (column == versionColumn) {
                    Version version = versionColumn.getEntityField().getAnnotation(Version.class);
                    String versionClass = version.nextVersion().getCanonicalName();
                    //version = ${@tk.mybatis.mapper.version@nextVersionClass("versionClass", version)}
                    sql.append(column.getColumn())
                            .append(" = ${@tk.mybatis.mapper.version.VersionUtil@nextVersion(")
                            .append("@").append(versionClass).append("@class, ")

.append(StringUtil.isEmpty(entityName)?"":entityName+".") //修复BUG
                            .append(column.getProperty()).append(")},");
                } else if (notNull) {
                    sql.append(SqlHelper.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", notEmpty));
                } else {
                    sql.append(column.getColumnEqualsHolder(entityName) + ",");
                }
            } else if(column.isId() && column.isUpdatable()){
                //set id = id,
                sql.append(column.getColumn()).append(" = ").append(column.getColumn()).append(",");
            }
        }
        sql.append("</set>");
        return sql.toString();
    }

评论 (7)

吕树临疯 创建了任务

加上这句就可修复:
.append(StringUtil.isEmpty(entityName)?"":entityName+".") //修复BUG

能不能提个PR已经对应的测试?

我也觉得这里有bug,不知道会不会修复

貌似批量更新是没法使用乐观锁的

abel533 置顶等级设置为
abel533 添加了
 
bug
标签

同样遇到了、

abel533 任务状态待办的 修改为已完成
abel533 置顶等级 修改为不置顶

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
8363 free 1578914558
Java
1
https://gitee.com/free/Mapper.git
git@gitee.com:free/Mapper.git
free
Mapper
Mapper

搜索帮助