-
Notifications
You must be signed in to change notification settings - Fork 84
Models
Models in JOhm are the fundamental persistable entites in Redis. They are Java classes that are annotated by the @Model annotation and further have selectable persistable fields decorated with JOhm annotations. Transient fields may not be decorated with JOhm annotations to hint JOhm to ignore their persistence. JOhm does not support @Model with interfaces because Java interfaces are just design contracts and thus inherently incomplete.
Also, JOhm does not require @Model classes to implement Java's Serializable interface because Java's wire-level protocol has no bearing on Redis persistence whatsoever; at the same time, it does not prevent any sort of Model hierarchy or interface implementation. One of JOhm's goals is to allow Redis persistence of existing Model dictionaries using transparent annotation-decoration and without the need for invasive code or configuration changes.
Since JOhm auto-generates Model id's, we ask that users provide a simple placeholder for them in all classes decorated with @Model. Id's should be of Long type and decorated by @Id annotation. Id field name does not matter; only its type and annotation do. An accompanying getId() helps to read this auto-generated Id and this getter should also be provided. In case getId() is not provided on the Model, you can still use JOhm.getId(model) but it will result in a slight runtime performance penalty. setId() is neither needed nor encouraged in order to prevent user applications from an accidental overwrite of the Redis-generated Id.
Model Id's are first loaded and available from getId() after a Model's first encounter with Redis in the form of a JOhm.save(model)
An example of a model is:
@Model
class User {
@Id
private Long id;
@Attribute
private String name;
@Attribute
@Indexed
private int age;
@Reference
private Country country;
@CollectionList(of = Comment.class)
private List<Comment> comments;
@CollectionSet(of = Item.class)
private Set<Item> purchases;
}
Dealing with IDs is easy. For example:
User user = new User();
user.setName("foo");
JOhm.save(user);
Long userId = user.getId();