lundi 10 mai 2010

Mapping Hibernate entities in Flex

When working with entities that have been loaded or persisted with Hibernate, collections (such as the Map) are instanciated using Hibernate's own implementations (like the PersistentMap).
If you want to serialize and deserialize all Java Maps to a Flex custom class, this is an issue (as you may have PersistentMaps and HashMaps both returned from the server, simply depending on how they where created).

The following article gives the basic idea:

In my case, I used a custom Map implementation, called ExternalMap, that is used to transfer map data over BlazeDS (so that I can map it to my custom HashCollection on the client side).
In the HibernatePropertyProxy mentioned in the article, I simply overrode the following method:

  public Object getInstanceToSerialize(Object instance) {
    // TODO Auto-generated method stub
    Object instanceToSerialize = null;
    if (instance instanceof Map) {
      instanceToSerialize = new ExternalMap();
      PersistentMap map = (PersistentMap)instance;
      for (Object key : map.keySet()) {
        ((Map)instanceToSerialize).put(key, map.get(key));
    else {
      instanceToSerialize = super.getInstanceToSerialize(instance);
    return instanceToSerialize;

Aucun commentaire:

Enregistrer un commentaire