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

[错误报告]: 乐观锁在实体反写的时候与实际值无法对应,或者说自动生成的乐观锁更新值存在问题 #6632

Open
3 tasks done
Stefankeep opened this issue Dec 12, 2024 · 1 comment

Comments

@Stefankeep
Copy link

确认

  • 我使用的版本是最新版, 并且使用插件确认过项目里无依赖版本冲突
  • 我已经在 issue 中搜索过, 确认问题没有被提出过
  • 我已经修改标题, 将标题中的 描述 替换为遇到的问题

当前程序版本

3.5.9

问题描述

当前环境

JDK21
SpringBoot 3.4.0
PostgreSQL 16

已配置LocalDateTime序列化

    public JacksonLocalDateTimeModule() {
        // 对于LocalDateTime的序列化和反序列化
        super(PackageVersion.VERSION);
        this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(
                DatePattern.NORM_DATETIME_PATTERN)));
        this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
        this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
        this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
        this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
        this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
    }

出现的问题

当发生更新时添加@VersionupdateTime,数据类型为LocalDateTime

生成的SQL

UPDATE iam_tenant SET create_time='2024-12-11T16:02:05', update_time='2024-12-12T11:00:58.736136700', create_user='zy', update_user='zy', iam_tenant_name='测试', iam_tenant_code='TEST1', iam_tenant_sort=1, iam_tenant_owner_role='67bf65dfd620129f422dfec0517a872c', iam_tenant_id_chain='d6d1c8bbe16446b2b3c12addbd355a92' WHERE id='d6d1c8bbe16446b2b3c12addbd355a92' AND update_time='2024-12-12T10:53:35'

会在更新时自动为updateTime赋值。

第二次进行修改时

UPDATE iam_tenant SET create_time='2024-12-11T16:02:05', update_time='2024-12-12T11:01:02.427611600', create_user='zy', update_user='zy', iam_tenant_name='测试', iam_tenant_code='TEST18', iam_tenant_sort=1, iam_tenant_owner_role='67bf65dfd620129f422dfec0517a872c', iam_tenant_id_chain='d6d1c8bbe16446b2b3c12addbd355a92' WHERE id='d6d1c8bbe16446b2b3c12addbd355a92' AND update_time='2024-12-12T11:00:58'

updateTime取到的作为版本号的值就缺少了秒后的八位
导致无法正常更新。

希望能够给出在哪里可以重写 自动更新实体的方法 或者LocalDateTime重写的问题。

详细堆栈日志

No response

@Stefankeep
Copy link
Author

找到目前乐观锁插件的映射如下:

private static class VersionFactory {
        private static final Map<Class<?>, Function<Object, Object>> VERSION_FUNCTION_MAP = new HashMap();

        private VersionFactory() {
        }

        public static Object getUpdatedVersionVal(Class<?> clazz, Object originalVersionVal) {
            Function<Object, Object> versionFunction = (Function)VERSION_FUNCTION_MAP.get(clazz);
            return versionFunction == null ? originalVersionVal : versionFunction.apply(originalVersionVal);
        }

        static {
            VERSION_FUNCTION_MAP.put(Long.TYPE, (Function)(version) -> (Long)version + 1L);
            VERSION_FUNCTION_MAP.put(Long.class, (Function)(version) -> (Long)version + 1L);
            VERSION_FUNCTION_MAP.put(Integer.TYPE, (Function)(version) -> (Integer)version + 1);
            VERSION_FUNCTION_MAP.put(Integer.class, (Function)(version) -> (Integer)version + 1);
            VERSION_FUNCTION_MAP.put(Date.class, (Function)(version) -> new Date());
            VERSION_FUNCTION_MAP.put(Timestamp.class, (Function)(version) -> new Timestamp(System.currentTimeMillis()));
            VERSION_FUNCTION_MAP.put(LocalDateTime.class, (Function)(version) -> LocalDateTime.now());
            VERSION_FUNCTION_MAP.put(Instant.class, (Function)(version) -> Instant.now());
        }
    }

当前的情况分析是,updateTime是LocalDateTime,但是采用了Timestamp作为更新实体的类型了?

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

No branches or pull requests

1 participant