grails - GORM 中的列名前缀

标签 grails grails-orm

对于每个项目,当我在 Grails 域类中使用 statususer 等属性时,我都会自动遇到保留 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/

相关文章:

grails - 从hudson运行 'run-app'命令

集合中的 Grails 域类

Grails 域对象包括类属性

java - Grails:使用 GORM 进行现场访问

grails - 我应该如何在 Grails 中编写 OptaPlanner Planning Entity PlanningVariable Annotation?

validation - 在 Grails 中对整数使用大小约束

security - Grails + Acegi : How to handle password renewal ? 登录与未登录用户

mysql - 如何让MySQL在grails 2.0上运行

Grails GORM 日期数据绑定(bind)

sql - 如何在 grails 中进行 Group By 以按 Count(*) 排序