Skip to content

Grails 8 GORM ignores NamingStrategy for hasMany join table column names (prefixed with entity class name) #15736

Closed

Description

@gsartori

Expected Behavior

In Grails 7 (GORM 9 / Hibernate 5.6), join table column names generated for hasMany associations do not consistently apply the configured NamingStrategy.

Instead of using the resolved table names, GORM uses the raw domain class names, including prefixes such as T, when generating join table foreign key columns.

Join table and foreign key columns should respect the naming strategy:

  • book_id
  • author_id

Findings

  • classToTableName() is invoked and works for entity tables
  • Join table generation for hasMany associations does not consistently apply NamingStrategy
  • FK column names are derived from domain class names instead of physical table names

Impact

When domain classes use naming conventions (e.g. T* prefix), schema generation becomes inconsistent:

  • entity tables are correctly renamed
  • join table FK columns still contain class prefixes

This forces the use of static mapping to create correct column names:

    static mapping = {
        team joinTable: [name: "book_authors", key: "author_id", column: "book_id"]
    }

Actual Behaviour

Join tables are created, but FK columns are derived from the raw entity class names, not the resolved table names:

  • tbook_id
  • tauthor_id

The 'T' prefix is not removed.

Steps To Reproduce

  1. Pull https://github.com/gsartori/issue-gorm-hasmany-fieldnames
  2. ./gradlew bootRun
  3. Look at the created tables in the database
  • author_books
  • book_authors

Environment Information

Grails 7.x
GORM 9.x
Hibernate 5.6.x
JDK 17+

Example Application

https://github.com/gsartori/issue-gorm-hasmany-fieldnames

Version

7.1.1

Activity

  1. jdaugherty commented on Jun 15, 2026

    @jdaugherty
    Contributor

    This is an interesting one since I'm using domain table names without any issue. I'll investigate this one to see how the example application is different from my application.

  2. gsartori commented on Jun 15, 2026

    @gsartori
    ContributorAuthor

    This behavior has been present for as long as I can remember (Grails 3.x), but since it wasn’t a priority for the Grails 7 release, I’ve postponed it.

    It would be great to have it working, as I’d like to continue using the T prefix in domain objects while removing it from the tables and columns in the generated database.

    In the code, it’s useful to recognize at a glance that we’re dealing with a class affected by a @Transactional context (hence the T).

  3. jamesfredley commented on Jun 16, 2026

    @jamesfredley
    Contributor

    I dug into this and can confirm the root cause. The custom NamingStrategy is honored for entity table names but ignored for the foreign-key column names generated inside hasMany/many-to-many join tables.

    The naming strategy exposes two separate methods, and the binder calls a different one depending on the path.

    Entity table names (works). In GrailsDomainBinder.getTableName() the binder uses the simple class name and routes through classToTableName():

    String shortName = domainClass.getJavaClass().getSimpleName();   // "TBook"
    tableName = getNamingStrategy(...).classToTableName(shortName);  // -> "book" (T stripped)

    Join-table FK columns (broken). The reproduction declares hasMany on both sides, so it's a many-to-many and binding flows through getForeignKeyForPropertyDomainClass():

    final String propertyName = NameUtils.decapitalize(property.getOwner().getName());
    return namingStrategy.propertyToColumnName(propertyName) + FOREIGN_KEY_SUFFIX;  // -> "...tbook_id"

    This path calls propertyToColumnName(), not classToTableName(). Since TNamingStrategy overrides only classToTableName(), the T-stripping logic is never invoked for FK columns, so the prefix survives. The plain (non-m2m) hasMany path has the same defect where it builds the element column name.

    There is also a secondary inconsistency: the table path feeds getSimpleName() while these FK paths feed getName() (fully-qualified), so even the input to the strategy differs between the two code paths.

    That mismatch is exactly why the static mapping joinTable override is currently required as a workaround.

    This lives in GORM's Hibernate binding (GrailsDomainBinder) and is present identically in both grails-data-hibernate7 and grails-data-hibernate5:

    Path Method Strategy method called Result
    getTableName() (line 1258/1265) uses getSimpleName() classToTableName() T stripped (correct)
    bindCollectionWithJoinTable() (line 770) uses getName() propertyToColumnName() T retained
    getForeignKeyForPropertyDomainClass() (line 3222) uses getName() propertyToColumnName() T retained

    A fix would make the FK-column derivation consistent with getTableName() (derive the FK prefix from the resolved table name / classToTableName(getSimpleName())). Worth noting it's a schema-affecting behavior change for existing apps that rely on the current column names, so it should land with a test and a clear note in the release docs.

  4. gsartori commented on Jul 1, 2026

    @gsartori
    ContributorAuthor

    @jamesfredley is there any chance we can have this in Grails 8?
    Unfortunately I will not be able to work on a PR at the moment.

  5. added a commit that references this issue on Jul 20, 2026
  6. changed the title [-]Grails 7 GORM ignores NamingStrategy for hasMany join table column names (prefixed with entity class name)[/-] [+]Grails 8 GORM ignores NamingStrategy for hasMany join table column names (prefixed with entity class name)[/+] on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions