NamingStrategy should support naming foreign keys and indexes

Description

hbm2ddl always generates foreign key constraints based on hashcodes, like this:
alter table apple add index FK365FA4BB24B1D45C (lemon_id), add constraint FK365FA4BB24B1D45C foreign key (lemon_id) references lemon (id);

Some databases don't support enough characters for foreign key names to generate a readable foreign key name, so that's a sensible default.
However, those of use that use databases like MySQL etc, would like a readable foreign key name, such as:
alter table apple add index FK_Apple_lemon (lemon_id), add constraint FK365FA4BB24B1D45C foreign key (lemon_id) references lemon (id);
We can do that by annotating every entity with @Index in JPA, or specifying it in a hbm file,
but we can't do it generally, by extending DefaultNamingStrategy or ImprovedNamingStrategy.

Here's a proposition on how we could do it:

  • In the NamingStrategy interface, next to the method foreignKeyColumnName, add a method:
    public String foreignKeyConstraintName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName

  • DefaultNamingStrategy and ImprovedNamingStrategy should implement it as it is now, based on hashcodes.

  • If a mapping has an @Index JPA annotation or it's specified in a hbm file, that override counts.

Activity

Show:

Brett MeyerOctober 4, 2013 at 3:36 AM

This was implemented under and will be in ORM 5.

Tim TweMay 21, 2009 at 8:18 PM

+1

This is important.

There certain database restrictions such as maximum table length introduce certain corporate standards that utilize reserved words, etc. The ability to provide a custom strategy will be invaluable.

@ForeignKey is sufficient but it is my understanding that this is hibernate only and does little when there is a mapped superclass with foreign keys as part of a many to one relationship.

Certain default strategies could be provided out of the box. The following example shows certain defaults that may be usefull:

@Entity
class AlphaBeta {
@ManyToOne Delta getDelta() { }
}

1. StandardNamingStrategy - FK365FA4BB24B1D45C

Utilize a unique hash

2. SimpleNamingStrategy - fk_alpha_beta_delta

Convert camel case names into underscore delimited names appending the method name

3. SimpleCountingNamingStrategy - fk_alpha_beta_1

Convert camel case names into underscore delimited names appending a method counter

  • Concern: What determines order? Order in the class, naming order, annotation?

4. VowelNamingStrategy - fk_alph_bt

Remove all vowels that are not on the leading edge of a camel case delimited by an underscore
While the total length is greater than 16, chop the last letter of each word starting with the longest and working from the right to the left

Geoffrey De SmetSeptember 29, 2008 at 3:55 PM

typo: Please replace "@Index" with "@ForeignKey" in the text above.

Duplicate

Details

Assignee

Reporter

Components

Priority

Created September 29, 2008 at 3:03 PM
Updated October 4, 2013 at 3:36 AM
Resolved October 4, 2013 at 3:36 AM