Interface DerivedPropertyDefinition<T>
- Type Parameters:
T- the underlying type
- All Superinterfaces:
AttributeDefinition<T>
A definition for a
Property which value is derived from the values of one or more attributes,
defined via Property.PropertyDefiner.derived(), or taken from a referenced entity, via
Property.PropertyDefiner.denormalized().
DerivedAttributeDefinition extends AttributeDefinition and configures attributes
that compute their values from other attributes within the same entity or from related entities.
These attributes provide calculated fields, formatting, aggregation, and other computed values.
Derived attributes can be cached for performance or computed on-demand:
interface Employee {
EntityType TYPE = DOMAIN.entityType("chinook.employee");
Column<Long> ID = TYPE.longColumn("id");
Column<String> FIRSTNAME = TYPE.stringColumn("firstname");
Column<String> LASTNAME = TYPE.stringColumn("lastname");
Column<LocalDate> HIREDATE = TYPE.localDateColumn("hiredate");
// Derived attributes
Property<String> NAME = TYPE.stringProperty("name");
Property<Integer> YEARS_EMPLOYED = TYPE.integerProperty("years_employed");
}
EntityDefinition employee() {
return Employee.TYPE.as()
.attributes(
Employee.ID.as()
.primaryKey(),
Employee.FIRSTNAME.as()
.column()
.caption("First name"),
Employee.LASTNAME.as()
.column()
.caption("Last name"),
Employee.HIREDATE.as()
.column()
.caption("Hire date"),
// Simple derived attribute (cached by default)
Employee.NAME.as()
.derived()
.from(Employee.FIRSTNAME, Employee.LASTNAME)
.with(values -> values.optional(Employee.FIRSTNAME).orElse("") + " " +
values.optional(Employee.LASTNAME).orElse(""))
.caption("Name"),
// Time-dependent derived attribute (not cached)
Employee.YEARS_EMPLOYED.as()
.derived()
.from(Employee.HIREDATE)
.with(values -> values.optional(Employee.HIREDATE)
.map(hireDate -> Period.between(hireDate, LocalDate.now()).getYears())
.orElse(null))
.cached(false) // Changes over time
.caption("Years employed"))
.build();
}
void employees(Entities entities) {
// Usage
Entity employee = entities.entity(Employee.TYPE)
.with(Employee.FIRSTNAME, "Jane")
.with(Employee.LASTNAME, "Peacock")
.with(Employee.HIREDATE, LocalDate.of(2002, 4, 1))
.build();
// Derived values are computed automatically
String name = employee.get(Employee.NAME); // "Jane Peacock" (cached)
Integer yearsEmployed = employee.get(Employee.YEARS_EMPLOYED); // Computed on each access
// Modifying source attributes updates derived values
employee.set(Employee.LASTNAME, "Park");
String newName = employee.get(Employee.NAME); // "Jane Park"
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceDerivedPropertyDefinition.DenormalizedBuilder<T, B extends DerivedPropertyDefinition.DenormalizedBuilder<T,B>> Builds a denormalized AttributeDefinition instancestatic interfaceDerivedPropertyDefinition.DerivedBuilder<T, B extends DerivedPropertyDefinition.DerivedBuilder<T,B>> Builds a derived AttributeDefinition instanceNested classes/interfaces inherited from interface AttributeDefinition
AttributeDefinition.Builder<T,B>, AttributeDefinition.DefaultValue<T> -
Field Summary
Fields inherited from interface AttributeDefinition
DECIMAL_SEPARATOR, FRACTION_DIGITS, GROUPING_SEPARATOR, LEXICAL_STRING_COMPARATOR, NUMBER_GROUPING, ROUNDING_MODE -
Method Summary
Methods inherited from interface AttributeDefinition
caption, comparator, dateTimeFormatter, dateTimePattern, derived, description, entityType, format, formatted, fractionDigits, hidden, mnemonic, roundingMode
-
Method Details
-
attribute
Description copied from interface:AttributeDefinitionTheAttributethis definition is based on, should be unique within an Entity. By default, theAttribute.name()serves as column name for database columns.- Specified by:
attributein interfaceAttributeDefinition<T>- Returns:
- the attribute this definition is based on
-
sources
-
value
DerivedValue<T> value()- Returns:
- the derived value
-
cached
boolean cached()Note that cached attribute values are included when an entity is serialized.- Returns:
- true if the value of this derived attribute is cached, false if computed on each access
-