gpt4 book ai didi

grails - GORM 中的列名前缀

转载 作者:行者123 更新时间:2023-12-02 11:51:29 28 4
gpt4 key购买 nike

对于每个项目,当我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com