Chapter 14. Non-Persistent Properties and POJOs

LinkRest maintains a model of all entities that can be exposed via REST. All persistent entities present in the underlying ORM (usually Cayenne) are automatically added to LinkRest model. What if you want to expose additional non-persistent properties of peristent objects or build entire request chains that are not based on persistent entities? There are three annotations to help with it: @LrAttribute or @LrRelationship and @LrId.

The first example is a typical Cayenne persistent class that has some transient properties:

import com.nhl.link.rest.annotation.LrAttribute;

// a typical Cayenne persistent class
public class Domain extends _Domain {

  @LrAttribute
  public String getLowercaseName() {
    return getName().toLowerCase();
  }
}

This one was simple. The second example is an entire POJO not known to Cayenne:

import com.nhl.link.rest.annotation.LrAttribute;
import com.nhl.link.rest.annotation.LrRelationship;

// a POJO not mapped in Cayenne
public class SomeClass {

  private int id;
  private String string;
  private SomeOtherClass related;

  @LrId
  public int getId() {
    return id;
  }
  
  @LrAttribute
  public String getString() {
    return string;
  }

  @LrRelationship 
  public SomeOtherClass getRelated() {
    return related;
  }
}

Creating and annotating a POJO was easy. But LinkRest still requires a backend that knows how to select and/or update those POJOs. Such custom "backends" can be configured per request chain using chain listener API. It is up to the caller what strategy the backend would utilize (maybe a REST call, or reading/writing from a NoSQL DB, etc.) :

// an object with methods annotated with one of the
// 'com.nhl.link.rest.annotation.listener' annotations
SomeCustomBackend altBackend = new SomeCustomBackend();

LinkRest.select(SomeClass.class, config).listener(altBackend).uri(urlInfo).select();