Menu

orm_logical_delete

Luis M. Villa

Logical deletion support

(Return to examples index)

If we want to add logical deletion support we only have to specify a global logical deletion attribute (or an entity attribute). For this matter, we use OrmConfiguration.setDefaultLogicalDeletionAttribute() and OrmConfiguration.setLogicalDeletionAttribute() methods:

@Override
protected void configureOrm(final OrmConfiguration ormConfig) {

    ormConfig
        .idAttributeIs("id")
        .idGenerationStrategyIs(new MySqlIdGenerationStrategy())
        .defaultLogicalDeletionAttributeIs("deleted")
        .entity(Issue.class)
            .idAttributeIs("issueId")
            .mapsToTable("issues")
            .mapsAttribute("issueId").with("id")
        .entity(User.class)
            .logicalDeletionAttributeIs("active")
            .mapsAttribute("id").with("userId");
}

Now, everytime we delete instances of a class that contains a "deleted" attribute (or a User with an "active" attribute), the row will not be deleted physically, but the column value set to true.

So, let's see an example:

// User supports logical deletion (has an "active" attribute)
Orm orm = new IssuesDbExamples().orm();
final User user = orm.save(new User("Luis", "Villa"));
final Integer userId = user.getId();
orm.delete(user);
final User userAfterDeletion = orm.loadById(User.class, userId);
final Boolean isDeleted = userAfterDeletion.getActive();
Assert.assertEquals(Boolean.TRUE, isDeleted); // It is not deleted, just updated

// Version does not support logical deletion (has not a "deleted" attribute)
final Version version = orm.save(new Version(02, 01));
final Integer versionId = version.getId();
orm.delete(version);
final Version versionAfterDeletion = orm.getById(Version.class, versionId);
Assert.assertNotNull(versionAfterDeletion); // It is deleted, not updated

... and, what if I want to delete physically an object although it supports logical deletion? Just use deletePhysical():

Orm orm = new IssuesDbExamples().orm();
final User user = orm.save(new User("Luis", "Villa"));
final Integer savedUserId = user.getId();
Integer deleted = orm.deletePhysical(user);
Assert.assertEquals(Integer.valueOf(1), deleted);
Assert.assertNull(orm.loadById(User.class, savedUserId));

Be aware that all entities that support logical deletion will have their "get" methods behaviour modified accordingly. When using a "get" method, the results will be filtered by the delete filed configured by the user. For retrieving all the objects, a "get---WithDeleted" method must be invocked. Let's look at one example:

final Orm orm = new IssuesDbExamples().orm();

// A user is deleted, so one user will have deleted column set to true
orm.deleteById(User.class, 1);

// Gets the list of users (filtered by deleted = false or null)
final List<User> list = orm.loadList(User.class);
Assert.assertEquals(Integer.valueOf(1), Integer.valueOf(list.size()));

// Gets the list of users (included deleted = true)
final List<User> listWithDeleted = orm.loadListWithDeleted(User.class);
Assert.assertEquals(Integer.valueOf(2), Integer.valueOf(listWithDeleted.size()));

Related

Wiki: Examples
Wiki: examples_toc
Wiki: orm_save

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.