Map domain class with predifined values to table
Is it possible to create domain class which will开发者_开发问答 be mapped to db table with predefined values, for example:
id value
1 test1
2 test2
3 test3
Or I have to manualy edit generated *.sql file to acomplish this?
Thanks in advance.
One way to have a static set of predefined rows in a table is to populate it in BootStrap.groovy
. For example:
def init = { servletContext ->
if (MyDomain.count() == 0) {
new MyDomain(id: 1, value: 'test1').save(failOnError: true)
new MyDomain(id: 2, value: 'test2').save(failOnError: true)
new MyDomain(id: 3, value: 'test3').save(failOnError: true)
}
}
Another approach would be to use the migrations plugin and create a database migration to populate the table.
精彩评论