Package io.objectbox.relation
Class ToOne<TARGET>
- java.lang.Object
-
- io.objectbox.relation.ToOne<TARGET>
-
- Type Parameters:
TARGET- target object type (@Entityclass).
- All Implemented Interfaces:
java.io.Serializable
public class ToOne<TARGET> extends java.lang.Object implements java.io.SerializableA to-one relation of an entity that references one object of aToOneentity.Example:
{@code // Java- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private BoxStoreboxStoreprivate booleancheckIdOfTargetForPutTo avoid calls togetTargetId(), which may involve expensive reflection.private booleandebugRelationsprivate java.lang.Objectentityprivate Box<java.lang.Object>entityBoxprivate RelationInfo<java.lang.Object,TARGET>relationInfoprivate longresolvedTargetIdprivate static longserialVersionUIDprivate TARGETtargetResolved target entity is cachedprivate Box<TARGET>targetBoxprivate longtargetIdprivate java.lang.reflect.FieldtargetIdFieldprivate booleanvirtualProperty
-
Constructor Summary
Constructors Constructor Description ToOne(java.lang.Object sourceEntity, RelationInfo<?,TARGET> relationInfo)In Java, the constructor call is generated by the ObjectBox plugin.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidclearResolved()Clears the target.private voidensureBoxes(TARGET target)booleanequals(java.lang.Object obj)TARGETgetCachedTarget()(package private) java.lang.ObjectgetEntity()For testsTARGETgetTarget()Returns the target object ornullif there is none.TARGETgetTarget(long targetId)If property backed, entities can pass the target ID to avoid reflection.longgetTargetId()private java.lang.reflect.FieldgetTargetIdField()inthashCode()voidinternalPutTarget(Cursor<TARGET> targetCursor)booleaninternalRequiresPutTarget()booleanisNull()booleanisResolved()booleanisResolvedAndNotNull()voidsetAndPutTarget(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.voidsetTarget(TARGET target)Prepares 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 Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
entity
private final java.lang.Object entity
-
relationInfo
private final RelationInfo<java.lang.Object,TARGET> relationInfo
-
virtualProperty
private final boolean virtualProperty
-
boxStore
private transient BoxStore boxStore
-
entityBox
private transient Box<java.lang.Object> entityBox
-
targetIdField
private transient java.lang.reflect.Field targetIdField
-
target
private TARGET target
Resolved target entity is cached
-
targetId
private long targetId
-
resolvedTargetId
private volatile long resolvedTargetId
-
checkIdOfTargetForPut
private boolean checkIdOfTargetForPut
To avoid calls togetTargetId(), which may involve expensive reflection.
-
debugRelations
private boolean debugRelations
-
-
Constructor Detail
-
ToOne
public ToOne(java.lang.Object sourceEntity, RelationInfo<?,TARGET> relationInfo)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 Detail
-
getTarget
public TARGET 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
@Internal public TARGET getTarget(long targetId)
If property backed, entities can pass the target ID to avoid reflection.
-
ensureBoxes
private void ensureBoxes(@Nullable TARGET target)
-
getCachedTarget
public TARGET 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:
setTarget(TARGET)
-
setAndUpdateTargetId
void setAndUpdateTargetId(long targetId)
-
setTarget
public void setTarget(@Nullable TARGET target)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:
setTargetId(long)
-
setAndPutTarget
public void setAndPutTarget(@Nullable TARGET target)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
public void setAndPutTargetAlways(@Nullable 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. Pass null to clear.When clearing the target entity, this does not remove it from its box. This only dissolves the relation.
-
setResolvedTarget
private void setResolvedTarget(@Nullable TARGET target, long targetId)Both values should be set (and read) "atomically" using synchronized.
-
clearResolved
private void clearResolved()
Clears the target.
-
getTargetId
public long getTargetId()
-
getTargetIdField
private java.lang.reflect.Field getTargetIdField()
-
internalRequiresPutTarget
@Internal public boolean internalRequiresPutTarget()
-
getEntity
java.lang.Object getEntity()
For tests
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-