is there a way to have hibernate use auto generated ids if it wasn't assigned but use the assigned value if it was?
views:
67answers:
1
+1
A:
I believe there is no out of the box solution in Grails for this but it should be fairly straightforward to implement your own org.hibernate.id.IdentifierGenerator.
Implementing that interface you would delegate to your default id generation strategy as long as no id is assigned and otherwise use the already assigned value of your domain object.
A simple implementation delegating to IdentityGenerator when no key is assigned could look like this:
package my.company.hibernate
import org.hibernate.engine.SessionImplementor
public class PreAssignedIdGenerator extends org.hibernate.id.IdentityGenerator {
public Serializable generate(SessionImplementor session, Object object) {
return object.id ? object.id : super.generate(session, object)
}
}
Your domain class would need to define the new id generator:
class FooDomain {
Long id
static mapping = {
id generator: "my.company.hibernate.PreAssignedIdGenerator"
}
}
codescape
2010-06-15 20:54:31