为Grails GORM字符串设置默认maxLength? [英] Setting default maxLength for Grails GORM Strings?

查看:262
本文介绍了为Grails GORM字符串设置默认maxLength?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以通过grails.gorm.default.constraints配置属性来设置默认约束:

  grails .gorm.default.constraints = {
'*'(可为空):
}

但有没有办法按类型设置它?我想要默认所有的字符串默认为maxSize:2000(主要是强制默认的数据库映射不是varchar(255))

解决方案

div>

我不认为在 Config.groovy 中可以轻松完成此操作。你可以创建一个自定义的hibernate的方言,但没有太多的麻烦。例如(使用Postgres方言):

  package mypackage; 

导入org.hibernate.dialect.PostgreSQLDialect;
import java.sql.Types;

public MyPostgresDialect扩展PostgresSQLDialect {
public MyPostgresDialect(){
super();
registerColumnType(Types.VARCHAR,text);




$ b然后更新 DataSource.groovy 使用新的方言:

 数据源{
...
dialect = mypackage.MyPostgresDialect
}


I know you can set default constraints via the grails.gorm.default.constraints config property by name by:

grails.gorm.default.constraints = {
    '*'(nullable:true)
}

but is there a way to set it by type? I want to default all my strings to default to maxSize:2000 (primarily to force the default db mapping to not be to varchar(255))

解决方案

I don't think there's any way to do this easily in Config.groovy. You can create a custom dialect for hibernate without too much trouble though. For example (using the Postgres dialect):

 package mypackage;

 import org.hibernate.dialect.PostgreSQLDialect;
 import java.sql.Types;

 public MyPostgresDialect extends PostgresSQLDialect {
     public MyPostgresDialect() {
         super();
         registerColumnType(Types.VARCHAR, "text");
     }
 }

Then update DataSource.groovy to use the new dialect:

datasource {
    ...
    dialect = mypackage.MyPostgresDialect
}

这篇关于为Grails GORM字符串设置默认maxLength?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆