hibernate - grails 2.4.3 升级后不必要的数据库 changeSet 与 boolean 相关

标签 hibernate postgresql grails grails-orm

我正在使用 postgres 数据库。升级到 grails 2.4.3 后,我得到了所有 bool 字段的这种类型的数据库变更集:

changeSet(author: "me(generated)", id: "1383573084784-1") {
    addColumn(tableName: "chapter") {
        column(defaultValue: true, name: "is_framable", type: "boolean") {
            constraints(nullable: "false")
        }
    }
}

isFramable 是域类 Chapter 中的一个 boolean 字段。即使在运行此迁移之后,它每次都会由 dbm-gorm-diff

生成

我注意到在旧版本的 grails 中,变更集中使用 bool 而不是 boolean

我正在使用 hibernate 版本 4.3.5.5

最佳答案

我的解决方法:

Config.groovy

grails.gorm.default.mapping = {
    "user-type" type: my.hibernate.type.BooleanBitType, class: boolean
    "user-type" type: my.hibernate.type.BooleanBitType, class: Boolean
}

BooleanBitType.java

import my.hibernate.type.descriptor.BooleanBitTypeDescriptor;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;

public class BooleanBitType extends org.hibernate.type.BooleanType {
    public static final BooleanBitType INSTANCE = new BooleanBitType();

    public BooleanBitType() {
        this(BooleanBitTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE);
    }

    protected BooleanBitType(SqlTypeDescriptor sqlTypeDescriptor, BooleanTypeDescriptor javaTypeDescriptor) {
        super(sqlTypeDescriptor, javaTypeDescriptor);
    }
}

BooleanBitTypeDescriptor.java

public class BooleanBitTypeDescriptor extends org.hibernate.type.descriptor.sql.BooleanTypeDescriptor {
    public static final BooleanBitTypeDescriptor INSTANCE = new BooleanBitTypeDescriptor();

    public BooleanBitTypeDescriptor() {
        super();
    }

    public int getSqlType() {
        return Types.BIT;
    }
}

关于hibernate - grails 2.4.3 升级后不必要的数据库 changeSet 与 boolean 相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26298295/

相关文章:

Grails 安全 : how to preprocess username (to make it case-insensitive)?

grails - 使用Activiti插件运行应用程序时发生异常

java - 使用 hibernate 连接数据库时出错

PostGresql:从另一个表的随机行复制数据

java - Spring Boot 1.4 单元测试 Hibernate 验证 WebMvcTest

ruby - 将 Redis 事务包装在数据库事务中

sql - 从命令 shell 处理 COPY 中的引号到 CSV

oracle - 将Spring Security与Oracle&Grails一起使用

java - 当对象未在 Hibernate 中延迟加载时发出警告

java - 使用 Spring 数据混淆 hibernate 行为