Class ToOne<TARGET>
- Type Parameters:
TARGET- target object type (@Entityclass).
- 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)withnullorsetTargetId(long)to0to 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BoxStoreprivate booleanTo avoid calls togetTargetId(), which may involve expensive reflection.private booleanprivate final Objectprivate final RelationInfo<Object, TARGET> private longprivate static final longprivate TARGETResolved target entity is cachedprivate longprivate Fieldprivate final boolean -
Constructor Summary
ConstructorsConstructorDescriptionToOne(Object sourceEntity, RelationInfo<?, TARGET> relationInfo) In Java, the constructor call is generated by the ObjectBox plugin. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidClears the target.private voidensureBoxes(TARGET target) boolean(package private) ObjectFor testsReturns the target object ornullif there is none.getTarget(long targetId) If property backed, entities can pass the target ID to avoid reflection.longprivate FieldinthashCode()voidinternalPutTarget(Cursor<TARGET> targetCursor) booleanbooleanisNull()booleanbooleanvoidsetAndPutTarget(TARGET target) Sets or clears the target entity and ID in the source entity, then puts the source entity to persist changes.voidsetAndPutTargetAlways(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.(package private) voidsetAndUpdateTargetId(long targetId) private voidsetResolvedTarget(TARGET target, long targetId) Both values should be set (and read) "atomically" using synchronized.voidPrepares to set the target object of this relation.voidsetTargetId(long targetId) Prepares to set the target of this relation to the object with the given ID.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
entity
-
relationInfo
-
virtualProperty
private final boolean virtualProperty -
boxStore
-
entityBox
-
targetBox
-
targetIdField
-
target
Resolved target entity is cached -
targetId
private long targetId -
resolvedTargetId
private volatile long resolvedTargetId -
checkIdOfTargetForPut
private boolean checkIdOfTargetForPutTo avoid calls togetTargetId(), which may involve expensive reflection. -
debugRelations
private boolean debugRelations
-
-
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 ornullif there is none.ToOneuses 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. -
ensureBoxes
-
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. Pass0to 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:
-
setAndUpdateTargetId
void setAndUpdateTargetId(long targetId) -
setTarget
Prepares to set the target object of this relation. Passnullto 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.
-
setResolvedTarget
Both values should be set (and read) "atomically" using synchronized. -
clearResolved
private void clearResolved()Clears the target. -
getTargetId
public long getTargetId() -
getTargetIdField
-
internalRequiresPutTarget
-
internalPutTarget
-
getEntity
Object getEntity()For tests -
equals
-
hashCode
public int hashCode()
-