primaryKey
EntityDefinition.PrimaryKey primaryKey()
- Returns:
- the
EntityDefinition.PrimaryKey
instance
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 DefaultDomain {
// 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.define(
Customer.ID.define()
.primaryKey(),
Customer.NAME.define()
.column()
.caption("Name")
.nullable(false)
.maximumLength(100),
Customer.EMAIL.define()
.column()
.caption("Email")
.maximumLength(255))
.tableName("customer")
.caption("Customer")
.orderBy(ascending(Customer.NAME))
.smallDataset(true)
.build();
}
}
static interface
static interface
static interface
static interface
static interface
static final PropertyValue<Boolean>
boolean
caption()
columns()
condition(ConditionType conditionType)
ConditionProvider
associated with the given type
// Define custom condition types in entity definition
interface Customer {
EntityType TYPE = DOMAIN.entityType("customer");
Column<String> STATUS = TYPE.stringColumn("status");
Column<LocalDate> LAST_ORDER_DATE = TYPE.localDateColumn("last_order_date");
// Custom condition for active customers
ConditionType ACTIVE = ConditionType.custom("activeCustomers");
}
// In domain definition
Customer.TYPE.define(
Customer.STATUS.define()
.column(),
Customer.LAST_ORDER_DATE.define()
.column())
.condition(Customer.ACTIVE, (columns, values) ->
// Returns customers with active status and recent activity
Condition.and(
Customer.STATUS.equalTo("ACTIVE"),
Customer.LAST_ORDER_DATE.greaterThanOrEqualTo(LocalDate.now().minusMonths(6))
))
.build();
// Usage
Condition activeCondition = Customer.ACTIVE.get();
List<Entity> activeCustomers = connection.select(activeCondition);
entity()
Entity
instance based on this definitionEntity
instance based on this definitionEntity
instance based on this definitionexists()
boolean
orderBy()
<T> Entity.Key
primaryKey(T value)
Entity.Key
instance based on this definition, initialised with the given valueboolean
readOnly()
boolean
type()
ConditionProvider
associated with the given type
// Define custom condition types in entity definition
interface Customer {
EntityType TYPE = DOMAIN.entityType("customer");
Column<String> STATUS = TYPE.stringColumn("status");
Column<LocalDate> LAST_ORDER_DATE = TYPE.localDateColumn("last_order_date");
// Custom condition for active customers
ConditionType ACTIVE = ConditionType.custom("activeCustomers");
}
// In domain definition
Customer.TYPE.define(
Customer.STATUS.define()
.column(),
Customer.LAST_ORDER_DATE.define()
.column())
.condition(Customer.ACTIVE, (columns, values) ->
// Returns customers with active status and recent activity
Condition.and(
Customer.STATUS.equalTo("ACTIVE"),
Customer.LAST_ORDER_DATE.greaterThanOrEqualTo(LocalDate.now().minusMonths(6))
))
.build();
// Usage
Condition activeCondition = Customer.ACTIVE.get();
List<Entity> activeCustomers = connection.select(activeCondition);
conditionType
- the condition typeIllegalArgumentException
- in case no ConditionProvider is associated with the given conditionType// Define custom string representation
Customer.TYPE.define(
Customer.ID.define()
.primaryKey(),
Customer.FIRST_NAME.define()
.column(),
Customer.LAST_NAME.define()
.column(),
Customer.EMAIL.define()
.column())
.stringFactory(customer ->
customer.get(Customer.LAST_NAME) + ", " +
customer.get(Customer.FIRST_NAME) +
" (" + customer.get(Customer.EMAIL) + ")")
.build();
// Usage
Entity customer = entities.builder(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 type