Setting default maxLength for Grails GORM Strings?

我只是一个虾纸丫 提交于 2019-12-10 13:56:20

问题


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))


回答1:


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
}



回答2:


Just to provide an additional answer I received from a co-worker - which wasn't applicable in this case, but might help others...

if you can follow a naming convention in your properties, then you could do a:

'*_s': (maxSize:2000)

I personally don't like cross-tying prop names and datatypes - but wanted to include this as an approach (even though I like the dialect answer by ataylor more...)



来源:https://stackoverflow.com/questions/19432543/setting-default-maxlength-for-grails-gorm-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!