4

How can I rename HIBERNATE_SEQUENCE? Methods for generating one sequence per table (and giving specific names to those sequences) are well-documented, but that's not what I'm looking for.

I don't mind having one sequence shared by all domain classes. I just need to give it an application-specific name.

3 Answers 3

2

There appears to be an open feature / enhancement request in the Hibernate JIRA to make this globally configurable: Make the default sequence name globally configurable. I believe, as a workaround, you would have to set the 'generator' attribute to the same name for all domain classes (defaults to hibernate_sequence) for every @Id field. See oracle sequence created.

As you hinted in your question, there might be a way to do this by subclassing your database dialect - as many have suggested for a sequence-per-table approach.

See id generator and DRY principle

Sign up to request clarification or add additional context in comments.

1 Comment

I seem to have had success extending org.hibernate.id.SequenceGenerator and the dialect.
2

Here is the code I used to set the sequence name.

First, the SequenceGenerator:

package com.foo; import java.util.Properties; import org.hibernate.MappingException; import org.hibernate.dialect.Dialect; import org.hibernate.id.SequenceGenerator; import org.hibernate.type.Type; public class TableNameSequenceGenerator extends SequenceGenerator { public static final String CUSTOM_SEQUENCE_NAME = "MYAPP_SEQUENCE" public void configure(Type type, Properties params, Dialect dialect) throws MappingException { if(params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) { String seqName = CUSTOM_SEQUENCE_NAME; params.setProperty(SEQUENCE, seqName); } super.configure(type, params, dialect); } } 

Next, the OracleDialect:

package com.foo; import org.hibernate.dialect.Oracle10gDialect; public class MyAppOracleDialect extends Oracle10gDialect { public Class getNativeIdentifierGeneratorClass() { return TableNameSequenceGenerator.class; } } 

Last, DataSource.groovy needs to know about the dialect:

dataSource { pooled = true driverClassName = "oracle.jdbc.OracleDriver" // username, password.... dialect='com.foo.MyAppOracleDialect' } 

Comments

0

Renaming the sequence is IMHO not directly possible but you might customize the identity as described on http://www.grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.5.2.4%20Custom%20Database%20Identity to generator:'native'. See http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-id-generator for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.