Class ToOne<TARGET>
- Type Parameters:
TARGET
- target object type (@Entity
class).
- All Implemented Interfaces:
Serializable
ToOne
entity.
Example:
// Java
@Entity
public class Order {
private ToOne<Customer> customer;
}
// Kotlin
@Entity
data class Order() {
lateinit var customer: ToOne<Customer>
}
Uses lazy initialization. The target object (getTarget()
) is only read from the database when it is first
accessed.
Common usage:
- Set the target object with
setTarget(TARGET)
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. setTargetId(long)
of the target object to create a relation.setTarget(TARGET)
withnull
orsetTargetId(long)
to0
to remove the relation.
Then, to persist the changes Box.put(T)
the object with the ToOne.
// Example 1: create a relation
order.getCustomer().setTarget(customer);
// or order.getCustomer().setTargetId(customerId);
store.boxFor(Order.class).put(order);
// Example 2: remove the relation
order.getCustomer().setTarget(null);
// or order.getCustomer().setTargetId(0);
store.boxFor(Order.class).put(order);
The target object is referenced by its ID.
This target ID (getTargetId()
) 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
.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionToOne
(Object sourceEntity, RelationInfo<?, TARGET> relationInfo) In Java, the constructor call is generated by the ObjectBox plugin. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns the target object ornull
if there is none.getTarget
(long targetId) If property backed, entities can pass the target ID to avoid reflection.long
int
hashCode()
void
internalPutTarget
(io.objectbox.Cursor<TARGET> targetCursor) boolean
boolean
isNull()
boolean
boolean
void
setAndPutTarget
(TARGET target) Sets or clears the target entity and ID in the source entity, then puts the source entity to persist changes.void
setAndPutTargetAlways
(TARGET target) Sets or clears the target entity and ID in the source entity, then puts the target (if not null) and source entity to persist changes.void
Prepares to set the target object of this relation.void
setTargetId
(long targetId) Prepares to set the target of this relation to the object with the given ID.
-
Constructor Details
-
ToOne
In Java, the constructor call is generated by the ObjectBox plugin.- Parameters:
sourceEntity
- The source entity that owns the to-one relation.relationInfo
- Meta info as generated in the Entity_ (entity name plus underscore) classes.
-
-
Method Details
-
getTarget
Returns the target object ornull
if there is none.ToOne
uses lazy initialization, so on first access this will read the target object from the database. -
getTarget
If property backed, entities can pass the target ID to avoid reflection. -
getCachedTarget
-
isResolved
public boolean isResolved() -
isResolvedAndNotNull
public boolean isResolvedAndNotNull() -
isNull
public boolean isNull() -
setTargetId
public void setTargetId(long targetId) Prepares to set the target of this relation to the object with the given ID. Pass0
to remove an existing one.To apply changes, put the object with the ToOne. For important details, see the notes about relations of
Box.put(Object)
.- See Also:
-
setTarget
Prepares to set the target object of this relation. Passnull
to remove an existing one.To apply changes, put the object with the ToOne. For important details, see the notes about relations of
Box.put(Object)
.- See Also:
-
setAndPutTarget
Sets or clears the target entity and ID in the source entity, then puts the source entity to persist changes. Pass null to clear.If the target entity was not put yet (its ID is 0), it will be put before the source entity.
-
setAndPutTargetAlways
Sets or clears the target entity and ID in the source entity, then puts the target (if not null) and source entity to persist changes. Pass null to clear.When clearing the target entity, this does not remove it from its box. This only dissolves the relation.
-
getTargetId
public long getTargetId() -
internalRequiresPutTarget
-
internalPutTarget
-
equals
-
hashCode
public int hashCode()
-