primaryKey
EntityDefinition.PrimaryKey primaryKey()
- Returns:
- the
EntityDefinition.PrimaryKeyinstance
An EntityDefinition contains all information about an entity's structure including:
Entity definitions are typically created using the builder pattern during domain initialization:
public class Store extends DomainModel {
// Define entity types
interface Customer {
EntityType TYPE = DOMAIN.entityType("store.customer");
Column<Integer> ID = TYPE.integerColumn("id");
Column<String> NAME = TYPE.stringColumn("name");
Column<String> EMAIL = TYPE.stringColumn("email");
}
// Define the entity structure
void defineCustomer() {
EntityDefinition definition = Customer.TYPE.as()
.attributes(
Customer.ID.as()
.primaryKey(),
Customer.NAME.as()
.column()
.caption("Name")
.nullable(false)
.maximumLength(100),
Customer.EMAIL.as()
.column()
.caption("Email")
.maximumLength(255))
.table("customer")
.caption("Customer")
.orderBy(ascending(Customer.NAME))
.smallDataset(true)
.build();
}
}
static interface static interface static interface static interface static interface static final PropertyValue<Boolean> booleancaption()EntityDefinition.Builder.caption(String), if any
the caption from the entity resource bundle, if a matching key is available
the entity type name (EntityType.name())
columns()condition(ConditionType conditionType) ConditionString associated with the given typeentity()Entity instance based on this definitionEntity instance based on this definitionEntity instance based on this definitionexists()booleanorderBy()<T> Entity.KeyprimaryKey(T value) Entity.Key instance based on this definition, initialised with the given valuebooleanreadOnly()booleantable()type()codion.db.optimisticLocking property on LocalEntityConnection.
ConditionString associated with the given typeconditionType - the condition typeConditionString associated with the given ConditionTypeIllegalArgumentException - in case no ConditionString is associated with the given ConditionTypeEntityDefinition.Builder.caption(String), if any
EntityType.name())
// Define custom string representation
Customer.TYPE.as()
.attributes(
Customer.ID.as()
.primaryKey(),
Customer.FIRST_NAME.as()
.column(),
Customer.LAST_NAME.as()
.column(),
Customer.EMAIL.as()
.column())
.formatter(customer ->
customer.get(Customer.LAST_NAME) + ", " +
customer.get(Customer.FIRST_NAME) +
" (" + customer.get(Customer.EMAIL) + ")")
.build();
// Usage
Entity customer = entities.entity(Customer.TYPE)
.with(Customer.FIRST_NAME, "John")
.with(Customer.LAST_NAME, "Doe")
.with(Customer.EMAIL, "john@example.com")
.build();
System.out.println(customer); // "Doe, John (john@example.com)"
EntityDefinition.Attributes instanceEntityDefinition.Columns instanceEntityDefinition.ForeignKeys instanceEntityDefinition.PrimaryKey instanceEntity instance based on this definitionEntity instanceEntity instance based on this definitionvalues - the initial values, an empty map in case of no valuesEntity instanceIllegalArgumentException - in case any of the value attributes are not part of the entity.Entity instance based on this definitionvalues - the initial values, an empty map in case of no valuesoriginalValues - the original values, an empty map in case of no original valuesEntity instanceIllegalArgumentException - in case any of the value attributes are not part of the entity.Entity.Key instance based on this definition, initialised with the given valueT - the key value typevalue - the key value, assuming a single value keyEntity.Key instanceIllegalStateException - in case the given primary key is a composite keyIllegalArgumentException - in case the value is not of the correct typeThe returned entity has no attribute values set and cannot be modified.
caption - the string to return when toString() is called