Skip to content

Instantly share code, notes, and snippets.

@animator013
Created October 13, 2016 09:01
Show Gist options
  • Save animator013/b6638f3e1531cf0b7111ae60927d3641 to your computer and use it in GitHub Desktop.
Save animator013/b6638f3e1531cf0b7111ae60927d3641 to your computer and use it in GitHub Desktop.
Grails 3.x.x Hibernate5 postgresql sequence per table
//config
dataSource {
driverClassName = "org.postgresql.Driver"
dialect = "db.MyPostgresqlDialect"
...
}
package db
class MyPostgresqlDialect extends PostgreSQL94Dialect {
@Override
Class getNativeIdentifierGeneratorClass() {
TableNameSequenceGenerator.class
}
@Override
String getNativeIdentifierGeneratorStrategy() {
return "db.TableNameSequenceGenerator";
}
}
package db
class TableNameSequenceGenerator extends SequenceStyleGenerator {
@Override
void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
if (!params.getProperty(SEQUENCE_PARAM)) {
String tableName = params.getProperty(TABLE)
if (tableName) {
params.setProperty(SEQUENCE_PARAM, "seq_${tableName}")
}
}
super.configure(type, params, serviceRegistry)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment