Given the following Grails GORM Domain Classes and using table-per-hierarchy inheritance:
class Book {
static belongsTo = [ parent: Parent ]
String title
}
abstract class Parent {
static hasMany = [ books: Book ]
}
class A extends Parent {
String asset
}
class B extends Parent {
String asset
}
Say I have retrieved an instance of class A from the database. I want to convert it to an instance of class B. What is the grails idiomatic way to do this?
Without the hasMany relation, I would just delete A and create a new B. But I don't want the overhead of going through a large number of Books and updating their parent_id fields to point to the new B.
Under the hood, I essentially just want to perform an SQL UPDATE to change the database field parent.class from A to B. So what's the recommended way to do this in GORM/Grails?