Classy Data

Two databases? Start with persistence.xml

Hello Two Dbs

AndroidHelloTwoDbs is based on the same-named ORMLite example. Three versions of this application are presented to make the following points about Classy Data persistence:
  1. Adding a second database requires only adding another persistence unit to persistence.xml
  2. Developing a pure Java database implementation and then porting it to Android is not only doable with Classy Data, it's desirable
  3. Classy Data provides flexibility in how to deal with database creation and upgrade events, but simple cases require minimal effort
This boils down to focusing on the application rather than expending effort on developing and maintaining database boilerplate code.

Java

Classy Data hello-two-dbs-example Java project contains version 1. of the application in package au.com.cybersearch2.example and associated resources are in src/main/resources project folder.
Version 2. of the application adds a "quote" field to both sample entity classes. It's in package au.com.cybersearch2.example.v2 and associated resources are in src/test/resources project folder. In the same package in class HelloTwoDbsUpgradeTest, there is also a Junit test which runs the Version 1 application, dynamically upgrades the database to Version 2 and then runs the v2. application.
hello_2_dbs

Run HelloTowDbsMain as a Java application, and you will see console output similar to that in the original ORMLite example. Upon successful completion, there will be 2 SQLite databases named helloTwoDb1.db and helloTwoDb2.db in the resources/db project folder. If you look at the Persistence Unit configurations in persistence.xml, you can see where these names come from. Note how each Entity class is declared using full package name too:

<persistence-unit name="simple">
<class>au.com.cybersearch2.example.SimpleData</class>
<property name="database-name" value="helloTwoDb1.db"/>
<property name="database-version" value="1"/>
</persistence-unit>
<persistence-unit name="complex">
<class>au.com.cybersearch2.example.ComplexData</class>
<property name="database-name" value="helloTwoDb2.db"/>
<property name="database-version" value="1"/>
</persistence-unit>