-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: CollectionOfElements Map impl HashMap/PersistentMap
PostPosted: Wed Aug 06, 2008 4:56 pm 
Newbie

Joined: Thu Nov 30, 2006 3:08 pm
Posts: 5
Hibernate version: 3.2.2

I have an entity bean with a Map<String,String> property that is persisted using @CollectionOfElements on the field. I had a problem where clients that were communicating via RMI to my server got ClassNotFoundExceptions on PersistentMap, since they didn't have hibernate in their classpath. So, I set about figuring out how to customize the Map implementation set into my entity by Hibernate.

I tried changing my entity to use method level annotations so I could swap in my own Map implementation in the setter. That did not work because apparently during entity retrieval the setter call happens BEFORE the PersistentMap is put into the session. The result was our old favorite: LazyInitializationException.

The solution I settled on was implementing the Java serialization method writeObject like so:

Code:
   private void writeObject(ObjectOutputStream out) throws IOException {
      if ( mapProperty != null
            && !mapProperty.getClass().equals(HashMap.class) ) {
         mapProperty = Maps.newHashMap(mapProperty);
      }
      out.defaultWriteObject();
   }


This seems to work. I was surprised that I could not find others having the same problem in this forum. I decided I should add a post for posterity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.