How to persist a Map field in CMP?

Discussions

EJB programming & troubleshooting: How to persist a Map field in CMP?

  1. How to persist a Map field in CMP? (1 messages)

    Hi,

    I'd like to know how to persist a field of strange type using CMP. For example, if i have a complex CMP bean which holds a field of Map or Hashtable type. How to persist this field?

    If the key and value are both of String type, i may define abstract accessor methods like:

    public abstract String getMapData();
    public abstract void setMapData();


    and then implement two business methods:

    public Map getMap() {
      String mapData = getMapData();
      // interprete the string and convert it into a Map.
      Map map = (new MapInterpretor()).interprete(mapData);
      return map;
    }
     
    public void setMap(Map map) {
      // similar implementation.
    }


    This approach may work but there are two disadvantages:
    1) An interpretor helper class should be implemented.
    2) Each time i call the getMap() method, the mapData string is parsed and interpreted. If this is a time-consuming process, how to avoid this as possible as i can?

    Another problem: if the Map's key or value is an arbitrary object, how to persist it? Serialize/deserialize it?

    Thanks for your suggestions in advance.
    heavyz
  2. 1) I don't think they will be any faster way to parse the string into Map.

    2) You can return a Synchronized Map by calling Collections.SynchronizedMap(new HashMap());