Annotation Type Formula2


@Target({FIELD,ANNOTATION_TYPE}) @Retention(RUNTIME) public @interface Formula2
Assign to a property to be based on a logical formula using property paths.

Unlike Formula which requires physical SQL with explicit table aliases and JOIN clauses, @Formula2 accepts a logical expression using dot-notation property paths. Ebean automatically resolves the required JOIN clauses and translates the paths to the appropriate table aliases at query time.

// Ebean automatically joins to the 'parent' association
@Formula2("coalesce(familyName, parent.familyName)")
private String effectiveFamilyName;

// Multi-level path: Ebean joins parent and parent.parent
@Formula2("coalesce(familyName, parent.familyName, parent.parent.familyName)")
private String deepEffectiveFamilyName;

The expression supports any SQL function whose arguments are logical property paths. The required JOIN clauses are added automatically when this property is included in a query.

Any property based on a formula becomes a read only property.

By default this formula property is included in queries (just like a normal mapped property, and consistent with Formula). Note that because a path based formula automatically adds the JOIN clauses it needs, every query that includes the property by default also pays the cost of those joins.

Combine @Formula2 with the @Transient annotation to make the property opt-in. The effect of @Transient is that the formula is NOT included in queries by default - it must be explicitly included via Query.select() or Query.fetch(). This is useful when the formula (or the joins it requires) is relatively expensive and you only want it included when explicitly requested.

  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The logical expression using property paths.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Optionally restrict this formula to specific database platforms.
  • Element Details

    • value

      String value
      The logical expression using property paths.

      For example: "coalesce(familyName, parent.familyName)"

    • platforms

      Platform[] platforms
      Optionally restrict this formula to specific database platforms.
      Default:
      {}