3

I am creating a web service with Jax-RS and GSON. Now, GSON can't convert HibernateProxy objects and I have some questions. I am having an API so that I can eager load and lazy load. When I want to eagerload I use Hibernate.initialize(...). However this does not seem to guarantee that a object is unproxied?

So where, when and how would you unproxy an object? And how do you deal with this when the object also may have releations to other objects that also need to be unproxied?

user626912
  • 2,406
  • 3
  • 21
  • 32
  • Are you working with [this guy](http://stackoverflow.com/questions/13432922/unproxy-hibernate-proxy-objects#comment18363348_13432922)? You have almost the same question. – ElderMael Nov 17 '12 at 21:46
  • 1
    Also, what is the exception thrown when you want GSON to convert a Hibernate Proxy? – ElderMael Nov 17 '12 at 21:46
  • Basically a duplicate of http://stackoverflow.com/questions/13459718/could-not-serialize-object-cause-of-hibernateproxy – Flavio Nov 29 '12 at 10:54

1 Answers1

2

Here is a sample code:

public <P> List<P> unproxy(Collection<P> objects) {
    Session session = sessionFactory.getCurrentSession();
    List<P> result = new ArrayList<P>();
    for (P t : objects) {
        Object unproxied = ((SessionImplementor)session).getPersistenceContext().unproxy(t);
        result.add((P) unproxied);
    }
    return result;
}
Nikita Koksharov
  • 8,672
  • 52
  • 63