* @see #persistWithSubobjects()
* @see #setupDatabase()
*/
@Test
public void simplePersistence() throws Exception {
SimplePerson personToPersist;
SimplePerson personFromDatabase;
/*
* The index is our persistence interface. It is described by the primary key data type (long),
* which refers to the ID entity, and the entity type itself.
*/
PrimaryIndex<Long, SimplePerson> primaryIndex;
/*
* Create example entity. Please have a look at that class and pay attention to its annotations
* that are required for that entity in order to be persisted and retrieved.
* Note that we do NOT specify the identifier (ID).
*/
{
personToPersist = new SimplePerson();
personToPersist.setLastName("Doe");
personToPersist.setFirstName("John");
}
/*
* All entities are identified by their primary key (ID). Therefore we create a
* typed index and persis the object. By using put() instead of putNoReturn(),
* the passed entity will be updated accordingly.
* A bit strange: out() also returns the entity in case of UPDATE, but it will
* be NULL for INSERT. FIXME
*/
{
primaryIndex = entityStore.getPrimaryIndex(Long.class, SimplePerson.class);
LOG.info("Object BEFORE persisting: " + personToPersist.toString());
primaryIndex.put(personToPersist);
LOG.info("Object AFTER persisting: " + personToPersist.toString()
+ " (now with ID!");
}
/*
* Retrieve object from database and compare if it equals the original one.
* We use the ID from the original update (put() returned the updated entity
* with the ID that we can use here...).
*/
{
final long id = personToPersist.getId();
personFromDatabase = primaryIndex.get(id);
assertNotNull(personFromDatabase);
LOG.info("Object retrieved from store: " + personFromDatabase.toString());
assertEquals(personToPersist.getLastName(), personFromDatabase.getLastName());
assertEquals(personToPersist.getFirstName(), personFromDatabase.getFirstName());
}
}