One of the great benefits of using an ORM product like JDXA is being able to use simple object-oriented APIs to handle persistence of model objects in your app. An app not only needs to save and update model objects in a database, but also occasionally needs to get rid of obsolete/unwanted objects from the database. This article describes how multiple objects along with their associated objects can easily be deleted from the database using JDXA ORM.
Consider TripMaster, a hypothetical Android app used to manage trip-related information. TripMaster has, among other classes, the following model classes: Trip, Destination, and Address. As shown in the class definitions below, Trip has a one-to-many relationship with Destination and Destination has a one-to-one relationship with Address.
Suppose you as an app developer want to give user an option to delete one or more Trip objects along with all of the related Destination and Address objects. Essentially, data from three underlying database tables will need to be deleted. One can retrieve a Trip object and the associated Destination objects and the associated Address objects and delete them one by one in reverse order. But is there an easier and faster way to do that with JDXA? Let’s explore the scenario in some detail.
Here are the representative model class definitions (in a package com.tripmaster.model).
Other constructors and setter/getter methods are not mentioned for brevity:
The objects of the above three classes are persisted in three different database tables as per the ORM specification defined below:
With the above class definitions and ORM specification, it is extremely easy to perform bulk deep delete operations with JDXA ORM. Since we have already defined a BYVALUE relationship between a trip and its destinations and between a destination and its address in the mapping specification, you can use JDXA’s delete2 method to deletethe entire object graph of the qualifying trip objects with one call.
Alternatively, if you already have one or more persistent objects in memory and want to delete them from the database, just pass those objects to the delete method.
JDXA also provides the option of dynamically specifying the portions of the object graph to perform a delete operation on (directed operations).
JDXA also supports bulk insert and bulk update operations along with many flexible and convenient query operations for your model objects.
Learn more about JDXA ORM, including how to get a free trial version, by visiting Software Tree’s website at http://www.softwaretree.com.