Skip to content

Anonymous Properties

btoddb edited this page Jan 20, 2011 · 3 revisions

Anonymous properties are Cassandra columns that exist, but do not have an @Column in the POJO. Here are some reasons they are required (and useful):

  • Your POJO could have the notion of “optional” properties or “dynamic” properties
  • Legacy columns in the ColumnFamily that must be preserved, but don’t map directly to a POJO property

Using the MyPojo example (listed in Using the EntityManager), you can see

private Map<String, String> anonymousProps = new HashMap<String, String>();

This is how MyPojo chooses to store its anonymous properties, but it could just as easily be with a Set or any other way that fits the need. The only requirement is that a Collection<Entry<String, String>> must be provided to the EntityManager when needed. There are two annotations on methods in the POJO:

 @AnonymousPropertyAddHandler
  public void addAnonymousProp(String name, String value) {
      anonymousProps.put(name, value);
  }

  @AnonymousPropertyCollectionGetter
  public Collection<Entry<String, String>> getAnonymousProps() {
      return anonymousProps.entrySet();
  }

These two methods are how the EntityManager sets and gets the anonymous properties. When saving a POJO the EntityManager will look for a method annotated with @AnonymousPropertyCollectionGetter, and the POJO must return the Collection of properties. When loading a POJO the EntityManager reads the Cassandra columns and tries to match column names with POJO properties using @Column. If no @Column match is found, the EntityManager will then look for a method annotated with @AnonymousPropertyAddHandler. If found the Column name/value is added to the POJO. If not found an IllegalArgumentException is thrown.

Clone this wiki locally