babyfish-ct / jimmer

A revolutionary ORM framework for both java and kotlin.
Apache License 2.0
876 stars 88 forks source link

关于Hibernate的@UpdateTimestamp,jimmer能变相实现这个功能吗 #712

Closed tzxylao closed 1 month ago

tzxylao commented 1 month ago

Hibernate有个@UpdateTimestamp注解,是框架自己管理的当对象发生变更的时候触发修改时间的变更 我是打算从Hibernate替换成jimmer,所以以前的注解希望尽可能兼容。我用的是postgresql数据库,对update_time字段数据库是没有设置触发器的 我尝试通过jimmer的beforeSave拦截器里设置修改时间,但这样会导致关联对象我按需修改字段时,即使没有变更字段,在拦截器也会设置上新的updateTime导致对象被修改,这里的实现机制和hibernate应该不一样吧,是否有办法实现和hibernate同样的效果呢?

@ApplicationScoped
@Unremovable
public class BaseDraftInterceptor implements DraftInterceptor<BaseEntity, BaseEntityDraft> {
    @Override
    public void beforeSave(@NotNull BaseEntityDraft draft, @Nullable BaseEntity original) {
        if (!ImmutableObjects.isLoaded(draft, BaseEntityProps.MODIFIED_TIME)) {
            draft.setModifiedTime(LocalDateTime.now());
        }
        if (original == null) {
            if (!ImmutableObjects.isLoaded(draft, BaseEntityProps.CREATED_TIME)) {
                draft.setCreatedTime(LocalDateTime.now());
            }
        }
    }

    @Nullable
    @Override
    public Collection<TypedProp<BaseEntity, ?>> dependencies() {
        return Arrays.asList(
                BaseEntityProps.CREATED_TIME,
                BaseEntityProps.MODIFIED_TIME);
    }
}
babyfish-ct commented 1 month ago

DraftInterceptor

babyfish-ct commented 1 month ago

Issue指BUG活FR,这类问题请放Discussions或加群