ToOne<EntityT> class
A to-one relation of an entity that references one object of a "target" entity EntityT
.
Example:
@Entity()
class Order {
final customer = ToOne<Customer>();
}
Uses lazy initialization. The target object is only read from the database when it is first accessed.
Common usage:
- set the target object to create a relation. When the object with the ToOne is put, if the target object is new (its ID is 0), it will be put as well. Otherwise, only the target ID in the database is updated.
- set the targetId of the target object to create a relation.
- set target to
null
or targetId to0
to remove the relation.
Then, to persist the changes Box.put the object with the ToOne.
// Example 1: create a relation
order.customer.target = customer;
// or order.customer.targetId = customerId;
store.box<Order>().put(order);
// Example 2: remove the relation
order.customer.target = null
// or order.customer.targetId = 0
store.box<Order>().put(order);
The target object is referenced by its ID. This targetId is persisted as part of the object with the ToOne in a special property created for each ToOne (named like "customerId").
To get all objects with a ToOne that reference a target object, see Backlink.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasValue → bool
-
Whether the relation field has a value stored. Otherwise it's null.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- target ↔ EntityT?
-
Returns the target object or
null
if there is none.getter/setter pair - targetId ↔ int
-
Get ID of a relation target object.
getter/setter pair
Methods
-
attach(
Store store) → void -
Initializes this relation, attaching it to the
store
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited