Interface Attribute<T>
- Type Parameters:
T- the attribute type
- All Known Subinterfaces:
Column<T>, ForeignKey, Property<T>
A named, typed value of an entity, of one of three kinds:
- a
Column, mapped to a table column - a
ForeignKey, referencing another entity - a
Property, a value the entity holds which its table does not
Attributes are the building blocks of entity definitions. Each attribute has a name, type, and is associated with a specific entity type.
Note that attribute names are case-sensitive and attributes of one kind are equal if their names and entityTypes are equal, the attribute value type does not factor into equality.
Attributes are created through the entity type factory methods and then configured
using the as() method of their kind 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");
// A property, not mapped to a column, here a derived one
Property<BigDecimal> TOTAL = TYPE.bigDecimalProperty("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:
-
Method Details
-
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
-