In Grails (GORM), how to override a constraint name

In Grails (GORM) , how to override a constraint name (in a generated dbm script). I am using Oracle with GORM. It seems that the length of the constraint name is limited to 15.

If there is no way to override, is there a way to change the length by more than 15 (say 25) !!

eg.

CREATE TABLE X ( 
       id NUMBER(19,0) NOT NULL, 
       CONSTRAINT overridden_name_here 
       PRIMARY KEY (id));   
+4
source share
2 answers

Not sure if the latest Grails 3 provides a more direct way to customize constraint names, but in Grails 2.4.5 and earlier, the path to it is a custom subclass.

GrailsAnnotationConfiguration secondPassCompile(). / , .

. Burt Beckwith: http://burtbeckwith.com/blog/?p=465

, , GORM.

+3

Grails 3, Hibernate 4.x, HibernateMappingContextConfiguration secondPassCompile . , Hibernate 5, . . graeme grails-:

. Hibernate 5 , . , , . Hibernate 5.2 .

, , Hibernate 5 Grails 3, . :

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitForeignKeyNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;

public class ReadableImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl {

    public static final ReadableImplicitNamingStrategy INSTANCE = new ReadableImplicitNamingStrategy();

    private String getPlural(String tableName) {

        final int len = tableName.length();
        final boolean isLower = Character.isLowerCase(tableName.charAt(len - 1));
        final String s = tableName.toLowerCase();
        final char lastChar = s.charAt(len - 1);

        String result = tableName;

        switch (lastChar) {
            case 'y':
                result = tableName.substring(0, tableName.length() -1) + (isLower? "ie": "IE"); break;
            case 's':
            case 'x':
            case 'z':
                result = tableName.substring(0, tableName.length() -1) + (isLower? "e": "E"); break;
            default:
                if (s.endsWith("sh")) {
                    result = tableName.substring(0, tableName.length() -1) + (isLower? "e": "E");
                }
        }
        result += (isLower? "s": "S");
        return result;
    }

    @Override
    public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {

        StringBuilder sb = new StringBuilder("FK_")
                .append(source.getReferencedTableName().getText())
                .append("_")
                .append(getPlural(source.getTableName().getText()));

        return toIdentifier(sb.toString(), source.getBuildingContext());

    }
}

, Gorm:

import org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration

    class GormConfiguration extends HibernateMappingContextConfiguration {

        @Override
        protected void reset() {
            super.reset();
            this.implicitNamingStrategy = ReadableImplicitNamingStrategy.INSTANCE;
        }

    }

, , application.yml , DataSource.

dataSource:
    dbCreate: update
    configClass: mypackage.GormConfiguration
+2

All Articles