Removing the proxy part of a grails domain object?

I want to get the actual instance of a domain object. That is, I need to serialize the object, and I'm trying to use the domain object on both sides of the httpinvoker chain. Is there a way to get a fully loaded domain object that does not have grails wiring, so that I can serialize it?

+7
source share
2 answers

We are running GrailsHibernateUtil.unwrapIfProxy(obj) . He won’t get rid of the introduced Grails methods and such - only the Hibernate / GORM proxy server, but that should be enough.

edit :

  • Sorry for the question, but did you declare your domain class as implements Serializable ?
  • It could be something you add / enter into your class, e.g. Grails non-bug 6379 .
  • This code fragment (got it here ) worked for me in 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() 
+8
source

According to the second comment in the answer here, explicitly deploying proxy classes using GrailsHibernateUtil.unwrapIfProxy requires another database call. I use HibernateProxyHelper.getClassWithoutInitializingProxy to achieve the same result, and I am sure that this does not make any additional database calls.

+1
source

All Articles