Axelor Doc

Methods "helper"

The Axelor platform automatically generates methods "helper" to manage the fields of type collection (List or Set for relationships O2M or M2M).

The generated methods are:

  • add - this method supports the initialization of the collection and ensure bi-directional relationship.
  • remove - this method takes care of removing the against-part if the field is an orphan.
  • clear - this method is the same as the helper remove, but on the whole collection.

For example, for an entity to contact , having a O2M to Address the following static methods will be generated in the class Contact :

Helpers methods
 /**
 * Add the given {@link #Address} item to the {@code addresses}.
 *
 * <p>
 * It sets {@code item.contact = this} to ensure the proper relationship.
 * </p>
 */
public void addAddress(Address item) {
    if (addresses == null) {
        addresses = new ArrayList<Address>();
    }
    addresses.add(item);
    item.setContact(this);
}
/**
 * Remove the given {@link #Address} item from the {@code addresses}.
 *
 */
public void removeAddress(Address item) {
    if (addresses == null) {
        return;
    }
    addresses.remove(item);
}
/**
 * Clear the {@code addresses} collection.
 *
 * <p>
 * It calls the {@code this.flush()} method to avoid unexpected errors
 * if any of the item in the collection is changed.
 * </p>
 */
public void clearAddresses() {
    if (addresses != null) {
        addresses.clear();
        this.flush();
    }
}

The following code:

Contact c = new Contact();
...
Address a1 = new Address();
a1.setContact(c);
...
c.setAddresses(Lists.newArrayList());
c.getAddresses().add(a1);

c.save();

may be reduced to:

Contact c = new Contact();
...
Address a1 = new Address();
...
c.addAddress(a1);

c.save();