I want to get at an actual instance of a domain object. That is, I need to serialize the object, and I'm trying to use the domain object on two sides of an httpinvoker
chain. Is there a way to get a fully-loaded domain object that doesn't have any grails wiring, so that I can serialize it?
We do GrailsHibernateUtil.unwrapIfProxy(obj)
. It won't get rid of Grails injected methods and such - only of Hibernate/GORM proxy, but it should be sufficient.
edit:
implements Serializable
?grails console
on a small domain class:.
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil
import com.somegroup.domain.*
def loc = SomeDomainClass.get(1)
loc = GrailsHibernateUtil.unwrapIfProxy(loc)
ByteArrayOutputStream bos = new ByteArrayOutputStream()
ObjectOutput out = new ObjectOutputStream(bos)
out.writeObject(loc)
byte[] yourBytes = bos.toByteArray()
According to the second comment in the answer here explicitly unwrapping a proxy classes using GrailsHibernateUtil.unwrapIfProxy
requires another database call. I have been using HibernateProxyHelper.getClassWithoutInitializingProxy to achieve the same result, and I'm pretty sure this does not make any extra database calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With