JPA

About JPA

VARS acts a front-end to a database. This allows users to easily enter and retrieve information from a database without having to know anything about databases. Internally, VARS uses Java objects to store information. These Java objects are mapped to database tables using JPA, an object/relational mapping tool.

Here is a list of the VARS Classes that are mapped to the database:

What do you, the developer, need to know about JPA?

For most deployments, you don't need to know anything about JPA. Internally, VARS uses the data access object (DAO) pattern to access the database. Each class has a corresponding DAO that is used for finding, inserting, deleting or updating data into the database The DAO classes are located in the following packages:

It is important to be aware that most database actions cascade. For example, if you have a VideoArchiveSet with several VideoArchives associated with it, when you insert the VideoArchiveSet the VideoArchives will automatically be inserted too. On the other hand, deletes cascade too. So deleting the VideoArchiveSet will also delete all the VideoArchives.

Using a DAO object

  1. Create an object to be persisted
    Observation observation = new Observation();
    observation.setConceptName('Nanomia');
    
  2. Get a DAO object that corresponds to your object. VARS uses Guice for dependency injection, so the full DI example is given here:
    Injector injector = Guice.createInjector(new vars.shared.InjectorModule("annotation-app"))
    ToolBelt toolBelt = injector.getInstance(vars.annotation.ui.ToolBelt)
    ObservationDAO dao = toolBelt.getAnnotationDAOFactory().newObservationDAO();
    
  3. Insert a new object into the database. Here we'll do it in it's own transaction
    dao.startTransaction();
    dao.persist(observation);
    dao.endTransaction();
    
  4. Update an object that already exists in the database
    dao.startTransaction();
    observation = dao.find(observation); // get the unmodified observation from the datbase and bring into the transaction
    observation.setConceptName('atolla'); // make a change. It's automatically updated when the transaction is ended
    dao.endTransaction()
    
  5. Delete an object's data from the database
    dao.startTransaction();
    observation = dao.find(observation);
    dao.remove(observation);
    dao.endTransaction();