Is it possible to assign Hibernate foreign key constraint name ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it possible to assign Hibernate foreign key constraint name ?

potter_ru
Hello.

We use Grails 1.1 with Liquibase plugin and PostgreSQL 8.3.7.
In Liquibase changelog.xml we described our foreign keys using such convention:

table_from + "_fk_" + table_to

But after running tests in test database (with dbCreate="update") we found that
hibernate have created additional foreign key constraints named

FK + hash(table_name) + hash(column_name).

I know that hibernate supports customizing foreign key name using "foreign-key=some_name" in
hbm.xml, but we want to stay with GORM DSL, becouse it's readability matters for us.

Can we somehow configure foreign key naming in GORM mappings ?
Thanks in advance.

Cheers,
   Igor.
Justify your existence !
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign Hibernate foreign key constraint name ?

Mike Brevoort-2
Bummer this wasn't answered.  I now have the same question.  Anyone have some guidance here?

Thanks,
Mike

On Tue, May 12, 2009 at 6:03 AM, Igor E. Poteryaev <[hidden email]> wrote:

Hello.

We use Grails 1.1 with Liquibase plugin and PostgreSQL 8.3.7.
In Liquibase changelog.xml we described our foreign keys using such
convention:

table_from + "_fk_" + table_to

But after running tests in test database (with dbCreate="update") we found
that
hibernate have created additional foreign key constraints named

FK + hash(table_name) + hash(column_name).

I know that hibernate supports customizing foreign key name using
"foreign-key=some_name" in
hbm.xml, but we want to stay with GORM DSL, becouse it's readability matters
for us.

Can we somehow configure foreign key naming in GORM mappings ?
Thanks in advance.

Cheers,
  Igor.
--
View this message in context: http://www.nabble.com/Is-it-possible-to-assign-Hibernate-foreign-key-constraint-name---tp23501236p23501236.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign Hibernate foreign key constraint name ?

David Durham, Jr.
On Mon, Sep 28, 2009 at 1:40 PM, Mike Brevoort <[hidden email]> wrote:
> Bummer this wasn't answered.  I now have the same question.  Anyone have
> some guidance here?

Last time I checked, there was a feature request for this within
hibernate itself, but no one had implemented.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-3499

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign Hibernate foreign key constraint name ?

David Durham, Jr.
On Mon, Sep 28, 2009 at 1:42 PM, David Durham <[hidden email]> wrote:
> On Mon, Sep 28, 2009 at 1:40 PM, Mike Brevoort <[hidden email]> wrote:
>> Bummer this wasn't answered.  I now have the same question.  Anyone have
>> some guidance here?
>
> Last time I checked, there was a feature request for this within
> hibernate itself, but no one had implemented.
>
> http://opensource.atlassian.com/projects/hibernate/browse/HHH-3499


actually, now that I reread it, that ticket is for a "naming strategy"
you should still be able to specify a foreign key name individually
for each key you want a custom name for ..  I don't know how this is
done in Grails .. sorry.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to assign Hibernate foreign key constraint name ?

ChenteCaliente
In reply to this post by potter_ru

in your Datasource.groovy:
configClass = OverrideGrailsDomainConfiguration


the class:

import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration
import org.hibernate.mapping.Table
import org.hibernate.MappingException
import org.hibernate.cfg.ImprovedNamingStrategy
import org.hibernate.dialect.Dialect

class OverrideGrailsDomainConfiguration extends DefaultGrailsDomainConfiguration {

    void secondPassCompileForeignKeys(Table table, Set done) throws MappingException {
        overrideForeignKeyNames(table)
        super.secondPassCompileForeignKeys(table, done)
    }

    void overrideForeignKeyNames(Table table) {
        table.foreignKeyIterator.each {fk ->
            def fromTableName = table.name
            def fromColumnName = fk.columnIterator.next().name
            def toTableName = ImprovedNamingStrategy.INSTANCE.classToTableName(fk.referencedEntityName)
            def foreignKeyName = "FK_${fromTableName}_${fromColumnName}_REFS_${toTableName}".toLowerCase()
            fk.setName foreignKeyName
        }
    }


    // Exists to make IntelliJ happy since superclass has a getProperty() method that confuses Groovy
    String getProperty(String propertyName) {
        super.getProperty(propertyName)
    }
}


Igor E. Poteryaev wrote
Hello.

We use Grails 1.1 with Liquibase plugin and PostgreSQL 8.3.7.
In Liquibase changelog.xml we described our foreign keys using such convention:

table_from + "_fk_" + table_to

But after running tests in test database (with dbCreate="update") we found that
hibernate have created additional foreign key constraints named

FK + hash(table_name) + hash(column_name).

I know that hibernate supports customizing foreign key name using "foreign-key=some_name" in
hbm.xml, but we want to stay with GORM DSL, becouse it's readability matters for us.

Can we somehow configure foreign key naming in GORM mappings ?
Thanks in advance.

Cheers,
   Igor.