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:
// Define entity types
interface Genre {
EntityType TYPE = DOMAIN.entityType("chinook.genre");
Column<Long> ID = TYPE.longColumn("id");
Column<String> NAME = TYPE.stringColumn("name");
}
// Define the entity structure
EntityDefinition genre() {
return Genre.TYPE.as()
.attributes(
Genre.ID.as()
.primaryKey(),
Genre.NAME.as()
.column()
.caption("Name")
.nullable(false)
.maximumLength(120))
.caption("Genres")
.orderBy(ascending(Genre.NAME))
.formatter(Genre.NAME)
.smallDataset(true)
.build();
}
A definition a client receives from a server comes without what the server alone uses: the table names,
the select query, the condition strings and the optimistic locking setting, and for its columns the names,
expressions, converters and generators. The methods providing these throw IllegalStateException
in such a definition.
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()booleanreadOnly()booleantable()type()codion.db.optimisticLocking property on LocalEntityConnection.
IllegalStateException - in case this definition was received from a serverConditionString associated with the given typeconditionType - the condition typeConditionString associated with the given ConditionTypeIllegalArgumentException - in case no ConditionString is associated with the given ConditionTypeIllegalStateException - in case this definition was received from a serverEntityDefinition.Builder.caption(String), if any
EntityType.name())
IllegalStateException - in case this definition was received from a serverIllegalStateException - in case this definition was received from a serverIllegalStateException - in case this definition was received from a server// Define custom string representation
Customer.TYPE.as()
.attributes(
Customer.ID.as()
.primaryKey(),
Customer.FIRSTNAME.as()
.column()
.caption("First name"),
Customer.LASTNAME.as()
.column()
.caption("Last name"),
Customer.EMAIL.as()
.column()
.caption("Email"))
.formatter(customer ->
customer.get(Customer.LASTNAME) + ", " +
customer.get(Customer.FIRSTNAME) +
" (" + customer.get(Customer.EMAIL) + ")")
.build();
// Usage
Entity customer = entities.entity(Customer.TYPE)
.with(Customer.FIRSTNAME, "John")
.with(Customer.LASTNAME, "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 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.The returned entity has no attribute values set and cannot be modified.
caption - the string to return when toString() is called