Module is.codion.framework.domain
Interface Attribute<T>
- Type Parameters:
T
- the attribute type
- All Known Subinterfaces:
Column<T>
,DerivedAttribute<T>
,ForeignKey
public interface Attribute<T>
Typed
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 valueClass does not factor into equality.
Attributes are typically created through entity type factory methods and then configured
using the define()
method to create attribute definitions:
public class Store extends DefaultDomain {
interface Customer {
EntityType TYPE = DOMAIN.entityType("store.customer");
// Typed attribute creation
Column<Integer> ID = TYPE.integerColumn("id");
Column<String> NAME = TYPE.stringColumn("name");
Column<String> EMAIL = TYPE.stringColumn("email");
Column<LocalDate> BIRTH_DATE = TYPE.localDateColumn("birth_date");
Column<Boolean> ACTIVE = TYPE.booleanColumn("active");
// Transient attribute (not mapped to database)
Attribute<String> DISPLAY_NAME = TYPE.stringAttribute("display_name");
// Custom typed attribute
Attribute<CustomerStatus> STATUS = TYPE.attribute("status", CustomerStatus.class);
}
void defineCustomer() {
Customer.TYPE.define(
// Column attributes
Customer.ID.define()
.primaryKey(),
Customer.NAME.define()
.column()
.nullable(false)
.maximumLength(100),
Customer.EMAIL.define()
.column()
.nullable(false),
Customer.BIRTH_DATE.define()
.column(),
Customer.ACTIVE.define()
.column()
.defaultValue(true),
// Transient attribute
Customer.DISPLAY_NAME.define()
.attribute()
.derived(Customer.NAME, Customer.EMAIL)
.valueProvider(sourceValues ->
sourceValues.get(Customer.NAME) + " (" + sourceValues.get(Customer.EMAIL) + ")"),
// Custom typed attribute
Customer.STATUS.define()
.column()
.columnClass(String.class, CustomerStatus::valueOf))
.build();
}
}
// Usage with entities
Entity customer = entities.builder(Customer.TYPE)
.with(Customer.NAME, "John Doe")
.with(Customer.EMAIL, "john@example.com")
.with(Customer.ACTIVE, true)
.build();
// Type-safe value access
String name = customer.get(Customer.NAME); // String
Boolean active = customer.get(Customer.ACTIVE); // Boolean
LocalDate birthDate = customer.get(Customer.BIRTH_DATE); // LocalDate
// Attribute type information
Class<String> nameType = Customer.NAME.type().valueClass(); // String.class
boolean isNumerical = Customer.ID.type().isNumerical(); // true
boolean isTemporal = Customer.BIRTH_DATE.type().isTemporal(); // true
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
ProvidesAttributeDefinition.Builder
instances.static interface
Defines the data-type of an Attribute -
Method Summary
Modifier and TypeMethodDescriptionstatic <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> valueClass) Creates a newAttribute
, associated with the given entityType.define()
name()
type()
-
Method Details
-
define
Attribute.AttributeDefiner<T> define()- Returns:
- a
Attribute.AttributeDefiner
for this attribute
-
type
Attribute.Type<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
- theTypeReference
representing 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 namevalueClass
- the class representing the attribute value type- Returns:
- a new
Attribute
-