Skip to content

Object Entity Mapping API

Renze de Vries edited this page Jul 1, 2015 · 1 revision

In JasDB 1.1 we have introduced a Object Entity Mapping API which allows you to work with your own Java objects and not having to care about the JasDB SimpleEntity which is used in the background.

Define the mappings

The mappings used by the object mapper are simple annotations on top of your java beans getter or setters methods. See the following example for a simple TestEntity bean:

@JasDBEntity(bagName = "TEST_BAG")
public class TestEntity {
    private String id;
    private String firstName;
    private String lastName;
    private List<String> hobbies;
    private Map<String, String> properties;

    @Id
    @JasDBProperty
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    @JasDBProperty
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }

    @JasDBProperty
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }

    @JasDBProperty(name = "HobbyList")
    public List<String> getHobbies() { return hobbies; }
    public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; }

    @JasDBProperty
    public Map<String, String> getProperties() { return properties; }
    public void setProperties(Map<String, String> addressProperties) { this.properties = addressProperties; }
}

Persisting Entities

In order to store data using an annotated Java object the following code can be used

DBSession session = new LocalDBSession(); //new RestDBSession();
EntityManager entityManager = session.getEntityManager();

TestEntity entity = new TestEntity(null, "Renze", "de Vries", newArrayList("programming", "model building", "biking"), new ImmutableMap.Builder<String, String>()
                            .put("city", "Amsterdam")
                            .put("street", "Secret passageway 10")
                            .put("zipcode", "0000TT").build());

String id = entityManager.persist(entity).getInternalId();

Querying for entities

We have also mapped all Query capabilities in JasDB on top of the EntityManager and the API is available for all Query operations.

In order to retrieve a specific entity by Id use the following:

DBSession session = new LocalDBSession(); //new RestDBSession();
EntityManager entityManager = session.getEntityManager();
TestEntity foundEntity = entityManager.findEntity(TestEntity.class, id);

If you want to Query JasDB you can use the following examples:

//This will find all entities
List<TestEntity> allEntities = entityManager.findEntities(TestEntity.class, QueryBuilder.createBuilder());

//Get all people named Piet
QueryBuilder query = QueryBuilder.createBuilder().field("firstName").value("Piet");
List<TestEntity> peopleNamedPiet = entityManager.findEntities(TestEntity.class, query);

//Get all people but limit to maximum 2 results
List<TestEntity> twoPeople = entityManager.findEntities(TestEntity.class, QueryBuilder.createBuilder(), 2);

//Get all people start at 2 and get maximum 2 (Pagination of results)
List<TestEntity> peoplePage2 = entityManager.findEntities(TestEntity.class, QueryBuilder.createBuilder(), 2, 2);

QueryBuilder

As you can see in above examples all the query capabilities in the EntityManager are based on the QueryBuilder pattern which is also used in the regular API as described here: Java Client API

Clone this wiki locally