简体   繁体   English

Grails + mysql以大写形式创建表名和列名

[英]Grails + mysql Create table name and column name in Upper case

Creating mysql table from Grails domain class does not generates table and column name in Uppercase letters. 从Grails域类创建mysql表不会以大写字母生成表和列名称。 table names are created in lowercase . 表名以小写形式创建。 Even when doing reverse-engineer with table names in uppercase letters the domain class is generated in lowercase only. 即使使用大写字母对表名进行逆向工程,域类也只以小写形式生成。 How to created table with table and column name in uppercase ? 如何用大写的表和列名创建表?

You can customize table names with a custom NamingStrategy . 您可以使用自定义NamingStrategy自定义表名。 By default Grails uses an ImprovedNamingStrategy but you can use your as described in the docs: http://grails.org/doc/latest/guide/GORM.html#customNamingStrategy 默认情况下,Grails使用ImprovedNamingStrategy但你可以按照文档中的描述使用你的: http//grails.org/doc/latest/guide/GORM.html#customNamingStrategy

This subclass of ImprovedNamingStrategy will generate uppercase names: 这个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()
   }
}

Specify it in DataSource.groovy in the hibernate block: hibernate块的DataSource.groovy中指定它:

hibernate {
   ...
   naming_strategy = com.foo.bar.UppercaseNamingStrategy
}

I agree with Burt, You can add also the following code to change the generated column name in database to upper case by overriding the other methods 我同意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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM