You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Grails 8 GORM ignores NamingStrategy for hasMany join table column names (prefixed with entity class name) #15736
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:
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.
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).
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():
Join-table FK columns (broken). The reproduction declares hasMany on both sides, so it's a many-to-many and binding flows through getForeignKeyForPropertyDomainClass():
This path calls propertyToColumnName(), notclassToTableName(). 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 mappingjoinTable 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.
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
Activity
jdaugherty commentedon Jun 15, 2026
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.
gsartori commentedon Jun 15, 2026
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
Tprefix 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
@Transactionalcontext (hence theT).jamesfredley commentedon Jun 16, 2026
I dug into this and can confirm the root cause. The custom
NamingStrategyis honored for entity table names but ignored for the foreign-key column names generated insidehasMany/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 throughclassToTableName():Join-table FK columns (broken). The reproduction declares
hasManyon both sides, so it's a many-to-many and binding flows throughgetForeignKeyForPropertyDomainClass():This path calls
propertyToColumnName(), notclassToTableName(). SinceTNamingStrategyoverrides onlyclassToTableName(), theT-stripping logic is never invoked for FK columns, so the prefix survives. The plain (non-m2m)hasManypath 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 feedgetName()(fully-qualified), so even the input to the strategy differs between the two code paths.That mismatch is exactly why the
static mappingjoinTableoverride is currently required as a workaround.This lives in GORM's Hibernate binding (
GrailsDomainBinder) and is present identically in bothgrails-data-hibernate7andgrails-data-hibernate5:getTableName()(line 1258/1265)getSimpleName()classToTableName()Tstripped (correct)bindCollectionWithJoinTable()(line 770)getName()propertyToColumnName()TretainedgetForeignKeyForPropertyDomainClass()(line 3222)getName()propertyToColumnName()TretainedA 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.gsartori commentedon Jul 1, 2026
@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.
PR for: apache#15736
[-]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)[/+]Fix join-table foreign keys to use resolved entity table names (#15736)…