The Solution

Use Entity Context. Every entity instance allows you to attach additional context information. In fact, you can store a whole variety of different types of information, indexed by the class of the information.

In practice the context information is stored in a ContextContainer which is essentially a Map attached to the entity. The key of the Map is the Java Class of the stored information:

Figure 1. Manipulating entity context
...
  void someMethod(MyEntity entity) {

    // Get the string stored in the entity's context:
    String s = entity.getContextContainer().get(String.class);
    System.out.println(s);

    // Store an updated string in the entity context:
    s += " longer context";
    entity.getContextContainer().put(String.class, s);
  }

As will be clear from the code above, you can only have one String value stored at any given time in the entity context. It is up to you to make sure that you "own" any class that you use as context on an entity, and that it will not interfere with other customizations. In practice, it may be wise to define your own classes for use as context, rather than using built-in classes such as String.

What if you want to store a set or a list as context? The Java List is a built-in class, and there are no class literals for Lists of your own types, i.e. no List<MyClass>.class. You can use a TypeLiteral as a key in this case, and it will be distinct from Lists of any other type which may also be stored in the entity context:

Figure 2. Manipulating parameterized types in context
...
  void someMethod(MyEntity entity) {

    // Get the List<MyClass> stored in the entity's context:
    TypeLiteral<List<MyClass>> type =
      new TypeLiteral<List<MyClass>>() { /**/ };
    List<MyClass> list = entity.getContextContainer().get(type);
    System.out.println(list);
  }

The ContextContainer class lets you retrieve, set, or remove context information by Class or by TypeLiteral. When you set the contents of the context container (using the ContextContainer.put(Class) method) the previous contents of the context container for that class, if any, are returned.