7

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?

Stefan Kendall
  • 61,898
  • 63
  • 233
  • 391
  • possible duplicate of [Converting Hibernate proxy to real object](http://stackoverflow.com/questions/2216547/converting-hibernate-proxy-to-real-object) – Bozho Apr 11 '11 at 15:00

2 Answers2

10

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:

  1. Sorry for asking, but did you declare your domain class as implements Serializable?
  2. It might be something you add/inject into your class, like in Grails non-bug 6379.
  3. This piece of code (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()
Community
  • 1
  • 1
Victor Sergienko
  • 11,714
  • 2
  • 50
  • 78
  • Would this let you serialize and de-serialize the object as an instance of the domain class, sans grails? This is what I really need, and previous attempts to simply unwrap the proxy, as it were, have failed. I casually googled for the source of unwrapIfProxy to no avail. – Stefan Kendall Apr 13 '11 at 00:22
  • It must be something in your class. I tried serializing a simple domain class and it worked. – Victor Sergienko Apr 13 '11 at 08:04
  • Here's the src: `https://github.com/grails/grails-core/blob/master/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsHibernateUtil.java` – Miguel Ping Apr 13 '11 at 15:23
  • Seems like I get this now: `ClassNotFoundException: org.hibernate.collection.PersistentSet`. I'm going to try including the hibernate jar on the deserialization end and see what happens. – Stefan Kendall Apr 14 '11 at 20:14
  • Well yes, if you need to deserialize the object into something without Hibernate dependencies (which is going to be different object), you'll need to do something to Hibernate collections. Though, just using them as regular ones should work. – Victor Sergienko Apr 14 '11 at 20:59
  • Now I'm getting `java.lang.NoClassDefFoundError: org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer`. Is there a way in grails to get a fully-initialized object? I really can't have any of the objects returned by "lazy." – Stefan Kendall Apr 14 '11 at 21:00
  • 1
    Looks like Hibernate limits object fetch to a single query and so only loads one child collection at a time (https://forum.hibernate.org/viewtopic.php?f=1&t=958500&view=previous). So you'll need some sort of recursive initialization. Deep copy, as one answer here (http://stackoverflow.com/questions/182323/how-to-serialize-hibernate-collections-properly/182955#182955) suggests, would do the thing. – Victor Sergienko Apr 14 '11 at 21:18
  • `beanlib` seems to do exactly what I need, but I have all kinds of issues trying to use it with grails. Ugh. – Stefan Kendall Apr 15 '11 at 18:15
  • Had a similar problem using the method list() on a domain class. All the objects in the list were fine with the exception of the first one. It was a proxy object and all it's attributes were null. I applied unwrapIfProxy on all the elements of the list to get the proper list of objects I needed. – Zan Jan 06 '16 at 15:23
1

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.

Community
  • 1
  • 1
ubiquibacon
  • 9,834
  • 25
  • 101
  • 175
  • 1
    Great point! Though, I'm in doubt: will it **always** result in a query, or only if there are uninitialized fields? If the latter, then probably it's safer to use `unwrapIfProxy` most of the time. – Victor Sergienko Mar 24 '15 at 14:54