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.