对于每个项目,当我在 Grails 域类中使用 status
或 user
等属性时,我都会自动遇到保留 SQL 字的问题。
所以我总是要添加一个
static mapping = {
status column:'prefix_status'
}
到我的类(class)。
我现在想知道是否有一种简单的方法可以为所有列添加给定字符串的前缀?
如果没有任何开箱即用的东西,我想可以创建一个插件,自动在所有域类中注入(inject)这样的映射 - 有人可以向我指出一个代码示例,该示例可以在类发生变化时对其进行修改吗?
最佳答案
手册中已经回答了这个问题:
Object Relational Mapping (GORM) - Custom Naming Strategy
添加到DataSource.groovy
配置:
hibernate {
...
naming_strategy = com.myco.myproj.CustomNamingStrategy
}
自定义命名类(位于src/groovy/com/myco/myproj/CustomNamingStrategy.groovy
下):
package com.myco.myproj
import org.hibernate.cfg.ImprovedNamingStrategy
import org.hibernate.util.StringHelper
class CustomNamingStrategy extends ImprovedNamingStrategy {
String propertyToColumnName(String propertyName) {
"prefix_" + StringHelper.unqualify(propertyName)
}
}
关于grails - GORM 中的列名前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7659301/