Interface Attribute<T>
- Type Parameters:
T- the attribute type
- All Known Subinterfaces:
Column<T>,ForeignKey
Attribute representing a named, typed property of an entity.
Attributes are the building blocks of entity definitions, representing columns, foreign keys, derived values, or transient properties. Each attribute has a name, type, and is associated with a specific entity type.
Note that attribute names are case-sensitive and Attributes are equal if their names and entityTypes are equal, the attribute value type does not factor into equality.
Attributes are typically created through entity type factory methods and then configured
using the as() method to create attribute definitions:
interface InvoiceLine {
EntityType TYPE = DOMAIN.entityType("chinook.invoiceline");
// Typed columns, mapped to table columns
Column<Long> ID = TYPE.longColumn("id");
Column<BigDecimal> UNITPRICE = TYPE.bigDecimalColumn("unitprice");
Column<Integer> QUANTITY = TYPE.integerColumn("quantity");
// An attribute not mapped to a column, here a derived one
Attribute<BigDecimal> TOTAL = TYPE.bigDecimalAttribute("total");
}
EntityDefinition invoiceLine() {
return InvoiceLine.TYPE.as()
.attributes(
InvoiceLine.ID.as()
.primaryKey(),
InvoiceLine.UNITPRICE.as()
.column()
.caption("Price")
.nullable(false),
InvoiceLine.QUANTITY.as()
.column()
.caption("Quantity")
.nullable(false)
.defaultValue(1),
InvoiceLine.TOTAL.as()
.derived()
.from(InvoiceLine.QUANTITY, InvoiceLine.UNITPRICE)
.with(new InvoiceLineTotal())
.caption("Total"))
.build();
}
void attributes(Entities entities) {
// Usage with entities
Entity invoiceLine = entities.entity(InvoiceLine.TYPE)
.with(InvoiceLine.UNITPRICE, BigDecimal.valueOf(0.99))
.with(InvoiceLine.QUANTITY, 2)
.build();
// Type-safe value access
BigDecimal unitPrice = invoiceLine.get(InvoiceLine.UNITPRICE);
Integer quantity = invoiceLine.get(InvoiceLine.QUANTITY);
BigDecimal total = invoiceLine.get(InvoiceLine.TOTAL); // 1.98
// Attribute type information
Class<BigDecimal> priceType = InvoiceLine.UNITPRICE.type().get(); // BigDecimal.class
boolean numeric = InvoiceLine.QUANTITY.type().isNumeric(); // true
boolean decimal = InvoiceLine.UNITPRICE.type().isDecimal(); // true
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceProvidesAttributeDefinition.Builderinstances.static interfaceDefines the data-type of an Attribute -
Method Summary
Modifier and TypeMethodDescriptionas()as(AttributeTemplate<T> template) Returns anAttributeDefinition.Builderfor this attribute, configured by the given template.static <T> Attribute<T> attribute(EntityType entityType, String name, TypeReference<T> typeReference) Creates a newAttribute, associated with the given entityType.static <T> Attribute<T> attribute(EntityType entityType, String name, Class<T> type) Creates a newAttribute, associated with the given entityType.name()type()
-
Method Details
-
as
Attribute.AttributeDefiner<T> as()- Returns:
- a
Attribute.AttributeDefinerfor this attribute
-
as
Returns anAttributeDefinition.Builderfor this attribute, configured by the given template.The template is applied first, any subsequent configuration overriding it.
Note that a
ColumnhasColumn.as(ColumnTemplate)as well, so a template handed to a column must be typed, an inline lambda being ambiguous between the two.- Parameters:
template- the attribute template- Returns:
- a
AttributeDefinition.Builderconfigured by the given template
-
type
Attribute.DataType<T> type()- Returns:
- the attribute type
-
name
String name()- Returns:
- the name of this attribute.
-
entityType
EntityType entityType()- Returns:
- the entity type this Attribute is associated with
-
attribute
static <T> Attribute<T> attribute(EntityType entityType, String name, TypeReference<T> typeReference) Creates a newAttribute, associated with the given entityType.- Type Parameters:
T- the attribute type- Parameters:
entityType- the entityType owning this attributename- the attribute nametypeReference- theTypeReferencerepresenting the attribute value type- Returns:
- a new
Attribute
-
attribute
Creates a newAttribute, associated with the given entityType.- Type Parameters:
T- the attribute type- Parameters:
entityType- the entityType owning this attributename- the attribute nametype- the class representing the attribute value type- Returns:
- a new
Attribute
-