Created
October 13, 2016 09:01
-
-
Save animator013/b6638f3e1531cf0b7111ae60927d3641 to your computer and use it in GitHub Desktop.
Grails 3.x.x Hibernate5 postgresql sequence per table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//config | |
dataSource { | |
driverClassName = "org.postgresql.Driver" | |
dialect = "db.MyPostgresqlDialect" | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package db | |
class MyPostgresqlDialect extends PostgreSQL94Dialect { | |
@Override | |
Class getNativeIdentifierGeneratorClass() { | |
TableNameSequenceGenerator.class | |
} | |
@Override | |
String getNativeIdentifierGeneratorStrategy() { | |
return "db.TableNameSequenceGenerator"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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