Introduction to Reladomo

I just announced the new Spring 5 modules in REST With Spring:

1. Overview

Reladomo (formerly known as Mithra) is an object-relational mapping (ORM) framework for Java, developed at Goldman Sachs, currently released as an open-source project. The framework provides the features commonly needed from an ORM as well as some additional ones.

Let’s see some of the key features of Reladomo:

it can generate Java classes as well as DDL scripts

it is driven by metadata written in XML files

the generated code is extensible

the query language is object-oriented and strongly typed

the framework provides support for sharding (same schema, different datasets)

the support for testing is also included

it provides useful features like performant caching and transactions

In the following sections, we’ll see the setup and some basic examples of use.

2. Maven Setup

To start using the ORM, we need to add the reladomodependency to our pom.xml file:

The gen-reladomo task uses the provided MithraGenerator to create Java files based on the configuration in ReladomoClassList.xml file. We will take a closer look at what this file contains in a later section.

The tasks also have two properties that define the location of the generated files:

generatedDir – contains the classes which shouldn’t be modified or versioned

nonGeneratedDir – the generated concrete object classes which can be further customized and versioned

The database tables corresponding to the Java objects can either be created manually or automatically by using the DDL scripts generated by a second Ant task:

We can see above the entity is defined inside a root element called MithraObject. Then, we have specified the package, class, and name of the corresponding database table.

Each property of the type is defined using an Attribute element, for which we can state the name, Java type, and column name.

We can describe the relationships between objects using the Relationship tag. In our example, we have defined a one-to-many relationship between Department and Employee objects, based on the expression:

Employee.departmentId = this.id

The reverseRelationshipName attribute can be used to make the relationship bi-directional without defining it twice.

This is a simple file containing a list of entities for which classes will be generated based on the XML configuration.

4. Generated Classes

Now we have all the elements we need to start the code generation by building the Maven application using the command mvn clean install.

The concrete classes will be generated in the src/main/java folder in the specified package:

These are simple classes where we can add our custom code. For example, the Department class only contains a constructor which should not be removed:

public class Department extends DepartmentAbstract {
public Department() {
super();
// You must not modify this constructor. Mithra calls this internally.
// You can call this constructor. You can also add new constructors.
}
}

If we want to add a custom constructor to this class, it needs to call the parent constructor as well:

Each object can also be saved separately by calling the insert() method. In our example, it’s possible to use cascadeInsert() because we’ve added the relatedIsDependent=true attribute to our relationship definition.