Interface PojoRepository<T,ID extends Serializable>


public interface PojoRepository<T,ID extends Serializable>

PojoRepository is the central class for the Pojo Facade. It supports CRUD operations and search. Each PojoRepository instance operates on only one pojo class. Create new PojoRepository instances based on your custom pojo type like so:

    public class MyClass {
       @Id
        public Integer getMyId() {
            ...
        }
        public void setMyId(Integer integer) {
            ...
        }
        ...
    }
    ...
    DatabaseClient client = ...;
    PojoRepository<MyClass, Integer> myClassRepo =
        client.newPojoRepository(MyClass.class, Integer.class);

Where MyClass is your custom pojo type, and myId is the bean property of type Integer marked with the @Id annotation. The @Id annotation can be attached to a public field or a public getter or a public setter. The bean property marked with @Id must be a native type or Serializable class and must contain an identifier value unique across all persisted instances of that type or the instance will overwrite the persisted instance with the same identifier.

The current implementation of the Pojo Facade uses Jackson databind for serialization and deserialization to and from json. Thus only classes which can be serialized and deserialized directly by Jackson can be serialized by the Pojo Facade. Every bean property including the one marked with @Id must either expose a public field or both a public getter and a public setter. To test if your class can be directly serialized and deserialized by Jackson, perform the following:

    ObjectMapper objectMapper = new ObjectMapper();
    String value = objectMapper.writeValueAsString(myObjectIn);
    MyClass myObjectOut = objectMapper.readValue(value, MyClass.class);

If that works but the configured objectMapper in the Pojo Facade is different and not working, you can troubleshoot by directly accessing the objectMapper used by the Pojo Facade using an unsupported internal method attached to the current implementation: com.marklogic.client.impl.PojoRepositoryImpl.

    ObjectMapper objectMapper = ((PojoRepositoryImpl) myClassRepo).getObjectMapper();

Special handling is provided for bean properties of type Date and Calendar. The current implementation of Jackson ObjectMapper changes the timezone to UTC. The serialized format is ISO 8601 format which is compatible with xs:dateTime format and can therefore be indexed in the server by a path range index of type dateTime. However, if you wish to query using a range index, it is recommended that you modify serialization to remove the class wrapper by adding a JsonTypeInfo annotation like so:

   @JsonTypeInfo(use=JsonTypeInfo.Id.NONE, include=JsonTypeInfo.As.EXTERNAL_PROPERTY)
    public Calendar getMyDateTime();

That way you can query the field directly without needing to traverse the java.util.GregorianCalendar type wrapper. By removing that wrapper, you can create a query like this:

    PojoQueryBuilder qb = pojoRepository.getQueryBuilder();
    PojoQueryDefinition query = qb.range("myDateTime", Operator.LT, Calendar.getInstance());

If your class has properties which are classes (non-native types) they will be automatically serialized and deserialized, but can only be written, read, or searched as properties of the parent instance. If you wish to directly write, read, or search instances of another class, create and use an instance of PojoRepository specific to that class.

Since PojoRepository stores in JSON format, which limits number precision to 15 significant digits (IEEE754 double precision), you will lose precision on numbers longer than 15 significant digits. If you desire larger numbers with no loss of precision, use Strings to persist those numbers.