Bulk Deep Delete Operations with JDXA ORM

BulkDelete
How can you make JDXA ORM delete multiple objects along with related objects (bulk deep delete)?

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:

public class Trip { // A trip with multiple destinations
private int tripId;
private String tripName;
private String purpose; // pleasure, business, …
List destinations;

public Trip() {
}
}

public class Destination { // In a trip
private int destinationId;
private int tripId;
private String city;
private String lodgingType; // friend, hotel, airbnb, …
private Address address;

public Destination() {
}
}

public class Address { // At a destination
private int destinationId;
private String addr1;
private String addr2;
private String city;
private String state;
private String zip;
private String Country;

public Address() {
}
}

The objects of the above three classes are persisted in three different database tables as per the ORM specification defined below:

JDX_OBJECT_MODEL_PACKAGE com.tripmaster.model
;
CLASS .Address
PRIMARY_KEY destinationId
SQLMAP FOR addr2 NULLABLE
;
CLASS .Destination
PRIMARY_KEY destinationId
RELATIONSHIP address REFERENCES .Address BYVALUE WITH destinationId
;
COLLECTION_CLASS ListDestinations COLLECTION_TYPE JAVACOLLECTION
ELEMENT_CLASS .Destination
PRIMARY_KEY tripId
;
CLASS .Trip
PRIMARY_KEY tripId
RELATIONSHIP destinations REFERENCES ListDestinations BYVALUE WITH tripId
;

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.
For example:

String tripClassName = Trip.class.getName();

// deep delete a particular trip (e.g., tripId = 5)
jdxHelper.delete2(tripClassName, “tripId=5”);

// deep delete many trips (e.g., tripId < 10)
jdxHelper.delete2(tripClassName, “tripId < 10”);

// deep delete all the trips
jdxHelper.delete2(tripClassName, null);

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.
For example:

List trips;
// initialize trips with one or more persistent objects
// deep delete all the given Trip objects
jdxHelper.delete(trips, true);

JDXA also provides the option of dynamically specifying the portions of the object graph to perform a delete operation on (directed operations).

Antibiotics was prescribed to my grandfather already twice. When the grandfather’s temperature rises and the prostatitis worsens, it means that the infection appeared in the body, and it needs to be eradicated only with antibiotics at https://mckesson.uk/antibiotics/. Already on the second day, the temperature fell.

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 https://www.softwaretree.com.

About Damodar Periwal

Damodar Periwal is the founder of Software Tree. He has an extensive background in databases, transaction processing, as well as distributed and object-oriented technologies. Damodar has more than 25 years of industry experience, having worked at leading companies like Google, TIBCO, Borland, Ashton-Tate, and Tandem Computers. Damodar has an MS (Computer Science) degree from University of Wisconsin (Madison) and an undergraduate engineering degree from Birla Institute of Technology and Science, Pilani (India).
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *