The short answer is no, there is no easy way. However, I found a solution that really works. Basically you need to implement your own dialect. Here is the implementation (note the source of the implementation in the comments).
package com.my.custom; import java.util.Properties; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.SequenceGenerator; import org.hibernate.type.Type; public class TableNameSequencePostgresDialect extends PostgreSQLDialect { @Override public Class<?> getNativeIdentifierGeneratorClass() { return TableNameSequenceGenerator.class; } public static class TableNameSequenceGenerator extends SequenceGenerator { @Override public void configure( final Type type, final Properties params, final Dialect dialect) { if (params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) { String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); if (tableName != null) { params.setProperty(SEQUENCE, "seq_" + tableName); } } super.configure(type, params, dialect); } } }
The above implementation should be stored as TableNameSequencePostgresDialect.java
under src/java/com/my/custom
in your Grails project.
Then update your DataSource.groovy
to use this new custom dialect.
dialect = com.my.custom.TableNameSequencePostgresDialect
This is pretty much about it. Not easy , but it can be done.
source share