Menu

Collection property is Empty without size()

Help
2008-12-12
2013-04-19
  • Andreas Mross

    Andreas Mross - 2008-12-12

    I have a POJO with a property that is a Set. Unless I call getSize() on the server before cloning, the Set comes across to the client empty.
    I am using dynamic proxy.

    From my debugging, the Set has members after retrieving from hibernate, but is empty after cloning.

    public class ClientServicesImpl extends HibernateRemoteService implements ClientServices {

    public Client getClient(String key)  {

    Client result = session.get(Client.class, key);
    result.getContacts().size(); // If I leave out this call, clonebean.getContacts() will be empty.
    Object clonebean = beanManager.clone(result);
    return clonebean;
    }

    Note that it doesn't work if I just call result.getContacts(); I have to call result.getContacts().size();

    Is there a reason for this behaviour? Am I doing something wrong or is this how it is supposed to work?

    Sorry if this is already in the documentation somewhere. I am new to GTW & Hibernate4GWT.

    Andreas

     
    • Andreas Mross

      Andreas Mross - 2008-12-19

      We are switching to Gilead. I will see if we have the same problem there, and if so I will repost this question in the Gilead forums.

      Cheers.

       
    • Bailey Peng

      Bailey Peng - 2009-01-04

      Hi,

      As a newbie to Hibernate4gwt, I too experienced this "weird behavior". However, after reading the documents more carefully I realized that this is considered normal behavior. The reason is simple. H4GWT nulls out all of the proxies/lazy-loaded properties of the retrieved object prior to sending it across the network back to your client side.
      In your case, the Set property of the retrieved object remained a Hibernate-injected proxy until you try to access the properties of the Set. That explains why you got the entire Set after you called size() on the Set and why you got a Null when you did not try to access any properties of the Set.
      The bottom line is this: if you have a lazy-loaded property in your retrieved object, it will be null unless you "touch" it. By touch, I mean access one of its properties.

      Hope this helps.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.