-
Notifications
You must be signed in to change notification settings - Fork 300
Anonymous Properties
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 persisting the data. 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 checks for a method annotated with @AnonymousPropertyAddHandler. If found, all columns will be retrieved from the ColumnFamily row and ones matching an @Column will be set in the POJO using its setter. All others are handled by the method that has @AnonymousPropertyAddHandler. If no @AnonymousPropertyAddHandler is found, then only POJO properties annotated with @Column will be retrieved from Cassandra, and any others will remain in the row.