Module is.codion.framework.domain
Interface AttributeDefinition<T>
- Type Parameters:
T- the underlying type
- All Known Subinterfaces:
ColumnDefinition<T>,DerivedAttributeDefinition<T>,ForeignKeyDefinition,TransientAttributeDefinition<T>,ValueAttributeDefinition<T>
public sealed interface AttributeDefinition<T>
permits ValueAttributeDefinition<T>, ForeignKeyDefinition (not exhaustive)
Defines an Attribute configuration including validation, formatting, and behavior settings.
AttributeDefinition instances specify how attributes behave within entities, including:
- Display properties (caption, description, format patterns)
- Validation rules (nullable, min/max values, length constraints)
- Default values and value generation
- UI behavior (items for combo boxes, comparators for sorting)
- Data conversion and formatting
AttributeDefinitions are created using the builder pattern through attribute definers:
public class Store extends DefaultDomain {
interface Product {
EntityType TYPE = DOMAIN.entityType("store.product");
Column<Integer> ID = TYPE.integerColumn("id");
Column<String> NAME = TYPE.stringColumn("name");
Column<String> DESCRIPTION = TYPE.stringColumn("description");
Column<BigDecimal> PRICE = TYPE.bigDecimalColumn("price");
Column<String> CATEGORY = TYPE.stringColumn("category");
Column<Boolean> ACTIVE = TYPE.booleanColumn("active");
Column<LocalDateTime> CREATED_DATE = TYPE.localDateTimeColumn("created_date");
}
void defineProduct() {
Product.TYPE.define(
Product.ID.define()
.primaryKey()
.generator(Generator.identity())
.caption("Product ID"),
Product.NAME.define()
.column()
.caption("Product Name")
.nullable(false)
.maximumLength(100)
.description("The name of the product"),
Product.DESCRIPTION.define()
.column()
.caption("Description")
.maximumLength(500)
.nullable(true),
Product.PRICE.define()
.column()
.caption("Price")
.nullable(false)
.minimum(BigDecimal.ZERO)
.maximum(new BigDecimal("99999.99"))
.fractionDigits(2)
.defaultValue(BigDecimal.ZERO),
Product.CATEGORY.define()
.column()
.caption("Category")
.nullable(false)
.items(List.of(
Item.item("ELECTRONICS", "Electronics"),
Item.item("CLOTHING", "Clothing"),
Item.item("BOOKS", "Books"),
Item.item("HOME", "Home & Garden"))),
Product.ACTIVE.define()
.column()
.caption("Active")
.nullable(false)
.defaultValue(true),
Product.CREATED_DATE.define()
.column()
.caption("Created")
.nullable(false)
.withDefault(true) // Database sets this
.updatable(false))
.build();
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAttributeDefinition.Builder<T,B extends AttributeDefinition.Builder<T, B>> Builds a attribute definition instancestatic interfaceSupplies values, for example default ones. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionTheAttributethis definition is based on, should be unique within an Entity.caption()Returns a string representation of the given value formatted with this attributes format.booleanbooleanhidden()charmnemonic()Returns the mnemonic associated with this attribute.booleannullable()voidValidates the value of this attribute as found in the given entity.voidValidates the value of this attribute as found in the given entity.
-
Field Details
-
MNEMONIC_RESOURCE_SUFFIX
The suffix used for the mnemonic resource key.- name=Name
- name.mnemonic=N
- See Also:
-
DESCRIPTION_RESOURCE_SUFFIX
The suffix used for the description resource key.- name=Name
- name.description=The customer name
- See Also:
-
-
Method Details
-
attribute
TheAttributethis definition is based on, should be unique within an Entity. By default, theAttribute.name()serves as column name for database columns.- Returns:
- the attribute this definition is based on
-
entityType
EntityType entityType()- Returns:
- the type of the entity this attribute is associated with
-
caption
String caption()- Returns:
- the caption or the attribute name if no caption has been specified
- See Also:
-
description
- Returns:
- a String describing this attribute or an empty Optional if none is available
-
format
Returns a string representation of the given value formatted with this attributes format. If no format is availableObject.toString()is used. By default, null values result in an empty string.- Parameters:
value- the value to format.- Returns:
- the value formatted as a string
-
hasDefaultValue
boolean hasDefaultValue()- Returns:
- true if a default value has been set for this attribute
-
defaultValue
- Returns:
- the default value for this attribute, if no default value has been set null is returned
- See Also:
-
nullable
boolean nullable()- Returns:
- true if null is a valid value for this attribute
-
mnemonic
char mnemonic()Returns the mnemonic associated with this attribute.- Returns:
- the mnemonic to use when creating a label for this attribute, 0 meaning no mnemonic
-
comparator
Comparator<T> comparator()- Returns:
- the Comparator to use when comparing values associated with this attribute
-
validate
Validates the value of this attribute as found in the given entity.Note: When validating non-nullable attributes during entity insertion (when the entity does not exist), null values are allowed for:
- Columns with default values - the database will provide the default value
- Generated primary key columns - the database will generate the key value
- Parameters:
entity- theEntitythe containing the value to validate- Throws:
ValidationException- in case of an invalid value
-
validate
Validates the value of this attribute as found in the given entity.Note: When validating non-nullable attributes during entity insertion (when the entity does not exist), null values are allowed for:
- Columns with default values - the database will provide the default value
- Generated primary key columns - the database will generate the key value
- Parameters:
entity- the entity containing the value to validatenullable- true if null values are allowed in this validation context, false if null should trigger aNullValidationException- Throws:
ValidationException- in case of an invalid value
-