简体   繁体   中英

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. 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 . 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

This subclass of ImprovedNamingStrategy will generate uppercase names:

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 {
   ...
   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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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