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

我在做created_at和updated_at自动生成的时候遇到了问题 #41

Open
lijialong1313 opened this issue Jan 14, 2024 · 2 comments

Comments

@lijialong1313
Copy link

在文档的事件中提到“允许你在模型生命周期中的多个时间点调用如下这些方法”。

然后我一开始直接复制文档代码,发现并不生效。然后在ORMEvent找到说明“其中以 record 为参数的事件, 仅在使用ORM风格时, 会触发”。

现在问题是这样的,在文档中并没有告知“使用ORM风格”是什么意思。我翻了Query和Model都没有提到ORM风格。且使用Query插入的a方式并不能触发creating和created。

我想询问的是是我使用方法有问题还是需要添加什么注解或者是我操作有什么问题,以及ORM风格是什么意思。

代码如下:
main:

        UserData userData=new UserData();
        userData.setNumber("123456");
        userDataModel.newQuery().insert(userData);

UserData(采用生成):

public class UserData extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /** auto generator start **/


    @Column(name = "number", length = 12L, comment = "学生:学号 老师:工号")
    private String number;

    @Column(name = "created_at", nullable = true)
    private LocalDateTime createdAt;

    @Column(name = "updated_at", nullable = true)
    private LocalDateTime updatedAt;

    @Column(name = "deleted_at", nullable = true)
    private LocalDateTime deletedAt;


    /** auto generator end **/

    @Repository
    public static class Model extends BaseEntity.BaseModel<UserData, Long> {
    }
}

BaseEntity(采用生成):

public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * auto generator start
     **/


    @Primary()
    @Column(name = "id", unsigned = true)
    private Long id;

    @Column(name = "created_at", nullable = true)
    private LocalDateTime createdAt;

    @Column(name = "updated_at", nullable = true)
    private LocalDateTime updatedAt;

    @Column(name = "deleted_at", nullable = true)
    private LocalDateTime deletedAt;

    /**
     * auto generator end
     **/
    public abstract static class BaseModel<T extends BaseEntity, K> extends Model<T, K> {

        @Resource
        @Lazy
        protected GaarasonDataSource gaarasonDataSource;

        @Override
        public GaarasonDataSource getGaarasonDataSource() {
            return gaarasonDataSource;
        }

        /**
         * sql日志记录
         *
         * @param sql           带占位符的sql
         * @param parameterList 参数
         */
        @Override
        public void log(String sql, Collection<?> parameterList) {
            String format = String.format(sql.replace(" ? ", "\"%s\""), parameterList.toArray());
            System.out.println(format);
            //log.debug("SQL complete : {}", format);
        }

        @Override

        public boolean creating(Record<T, K> record) {
            System.out.println("orm creating");
            return super.creating(record);
        }

        /**
         * 是否启用软删除
         */
        @Override
        protected boolean softDeleting() {
            return false;
        }

        /**
         * 删除(软/硬删除)
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        public int delete(Builder<T, K> builder) {
            return softDeleting() ? softDelete(builder) : builder.forceDelete();
        }

        /**
         * 恢复软删除
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        public int restore(Builder<T, K> builder) {
            return softDeleteRestore(builder);
        }

        /**
         * 软删除查询作用域(反)
         *
         * @param builder 查询构造器
         */
        @Override
        protected void scopeSoftDeleteOnlyTrashed(Builder<T, K> builder) {
            builder.whereNotNull("deleted_at");
        }

        /**
         * 软删除查询作用域(反)
         *
         * @param builder 查询构造器
         */
        protected void scopeSoftDeleteWithTrashed(Builder<T, K> builder) {

        }

        /**
         * 软删除查询作用域
         *
         * @param builder 查询构造器
         */
        @Override
        protected void scopeSoftDelete(Builder<T, K> builder) {
            builder.whereNull("deleted_at");
        }


        /**
         * 软删除实现
         *
         * @param builder 查询构造器
         * @return 删除的行数
         */
        @Override
        protected int softDelete(Builder<T, K> builder) {
            return builder.data("deleted_at", LocalDateTime.now()).update();
        }

        /**
         * 恢复软删除实现
         *
         * @param builder 查询构造器
         * @return 恢复的行数
         */
        @Override
        protected int softDeleteRestore(Builder<T, K> builder) {
            return builder.data("deleted_at", null).update();
        }
    }
}
@xutengx
Copy link
Member

xutengx commented Jan 15, 2024

你好
文档中所指的"ORM风格" 大致是 文档 的风格.
即, 操作由 Record 而非 Query 去执行.
这种, 可以触发 creating 和 created 等定义事件.

根据你的示例代码, 大致是如下的形式:

// 先获取新的 record
Record<UserData , Long> record = userDataModel.newRecord();

// 此处不应使用 toObject() 获取具体泛型对象
UserData userData = record.getEntity();

// 设置属性
userData.setNumber("123456");

// 保存
record.save();

文档中对此的表述, 会加以改进.

@xutengx
Copy link
Member

xutengx commented Jan 15, 2024

在 5.2.0 中, 修改了定义事件的相关的方法名, 使之更不易混淆
详见 文档

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

2 participants