JBoss.org Community Documentation

- Prev
- Next

## 3.6. Collection Mapping

Due to current Java limitations, Collection classes that implement `Set`, `List`, and `Map` are substituted with a Java proxy. That is, whenever POJO Cache encounters any Collection instance, it will:

- Create a Collection proxy instance and place it in the cache (instead of the original reference). The mapping of the Collection elements will still be carried out recursively as expected.
- If the Collection instance is referenced from another object, POJO Cache will swap out the original reference with the new proxy, so that operations performed by the refering object will be picked up by the cache.

The drawback to this approach is that the calling application must re-get any collection references that were attached. Otherwise, the cache will not be aware of future changes. If the collection is referenced from another object, then the calling app can obtain the proxy by using the publishing mechanism provided by the object (e.g. Person.getHobbies()). If, however, the collection is directly attached to the cache, then a subsequent `find()` call will need to be made to retrieve the proxy.

The following code snippet illustrates obtaining a direct Collection proxy reference:

    List list = new ArrayList();
    list.add("ONE");
    list.add("TWO");

    cache.attach("pojo/list", list);
    list.add("THREE"); // This won't be intercepted by the cache!

    List proxyList = cache.find("pojo/list"; // Note that list is a proxy reference
    proxyList.add("FOUR"); // This will be intercepted by the cache

This snippet illustrates obtaining the proxy reference from a refering object:

    Person joe = new Person();
    joe.setName("Joe Black"); // This is base class attributes
    List lang = new ArrayList();
    lang.add("English");
    lang.add("Mandarin");
    joe.setLanguages(lang);

    // This will map the languages List automatically and swap it out with the proxy reference.
    cache.attach("pojo/student/joe", joe);
    lang = joe.getLanguages(); // Note that lang is now a proxy reference
    lang.add("French"); // This will be intercepted by the cache

Finally, when a Collection is removed from the cache (e.g., via `detach`), you still can use the proxy reference. POJO Cache will just redirect the call back to the in-memory copy. See below:

    List list = new ArrayList();
    list.add("ONE");
    list.add("TWO");

    cache.attach("pojo/list", list);
    List proxyList = cache.find("pojo/list"); // Note that list is a proxy reference
    proxyList.add("THREE"); // This will be intercepted by the cache

    cache.detach("pojo/list"); // detach from the cache
    proxyList.add("FOUR"); // proxyList has 4 elements still.

- Prev3.5. Physical object cache mapping model
- Top of page
- Front page
- Next3.6.1. Limitations