从Grails域类创建mysql表不会以大写字母生成表和列名。表名以小写形式创建。即使使用大写字母对表名进行逆向工程,域类也只以小写形式生成。如何用大写创建表和列名的表?
答案 0 :(得分:3)
您可以使用自定义NamingStrategy
自定义表名称。默认情况下,Grails使用ImprovedNamingStrategy
,但您可以按照文档中的说明使用您的http://grails.org/doc/latest/guide/GORM.html#customNamingStrategy
ImprovedNamingStrategy
的这个子类将生成大写名称:
package com.foo.bar
import org.hibernate.cfg.ImprovedNamingStrategy
class UppercaseNamingStrategy extends ImprovedNamingStrategy {
private static final long serialVersionUID = 1
String classToTableName(String className) {
super.classToTableName(className).toUpperCase()
}
String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName).toUpperCase()
}
String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {
super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName).toUpperCase()
}
String tableName(String tableName) {
super.tableName(tableName).toUpperCase()
}
}
在DataSource.groovy
块中的hibernate
中指定它:
hibernate {
...
naming_strategy = com.foo.bar.UppercaseNamingStrategy
}
答案 1 :(得分:0)
我同意Burt,您还可以添加以下代码,通过覆盖其他方法将数据库中生成的列名更改为大写
public String propertyToColumnName(String propertyName) {
return super.propertyToColumnName(propertyName).toUpperCase();
}
public String columnName(String columnName) {
return super.columnName(columnName).toUpperCase();
}
public String joinKeyColumnName(String joinedColumn, String joinedTable) {
return super.joinKeyColumnName( joinedColumn, joinedTable ).toUpperCase();
}
public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
return super.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName, referencedColumnName).toUpperCase();
}
public String logicalColumnName(String columnName, String propertyName) {
return super.logicalColumnName(columnName, propertyName).toUpperCase();
}
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return logicalCollectionColumnName(columnName, propertyName, referencedColumn).toUpperCase();
}