Class ToOne<TARGET>

java.lang.Object
io.objectbox.relation.ToOne<TARGET>
All Implemented Interfaces:
Serializable

public class ToOne<TARGET> extends Object implements Serializable
Manages a to-one relation: resolves the target object, keeps the target Id in sync, etc. A to-relation is unidirectional: it points from the source entity to the target entity. The target is referenced by its ID, which is persisted in the source entity.

If there is a ToMany relation linking back to this to-one relation (@Backlink), the ToMany object will not be notified/updated about persisted changes here. Call ToMany.reset() so it will update when next accessed.

See Also:
  • Constructor Details

    • ToOne

      public ToOne(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 Details

    • getTarget

      public TARGET getTarget()
      Returns:
      The target entity of the to-one relation.
    • getTarget

      @Internal public TARGET getTarget(long targetId)
      If property backed, entities can pass the target ID to avoid reflection.
    • getCachedTarget

      public TARGET getCachedTarget()
    • isResolved

      public boolean isResolved()
    • isResolvedAndNotNull

      public boolean isResolvedAndNotNull()
    • isNull

      public boolean isNull()
    • setTargetId

      public void setTargetId(long targetId)
      Sets or clears the target ID in the source entity. Pass 0 to clear.

      Put the source entity to persist changes. If the ID is not 0 creates a relation to the target entity with this ID, otherwise dissolves it.

      See Also:
    • setTarget

      public void setTarget(@Nullable TARGET target)
      Sets or clears the target entity and ID in the source entity. Pass null to clear.

      Put the source entity to persist changes. If the target entity was not put yet (its ID is 0), it will be stored when the source entity is put.

      See Also:
    • 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.

    • getTargetId

      public long getTargetId()
    • internalRequiresPutTarget

      @Internal public boolean internalRequiresPutTarget()
    • internalPutTarget

      @Internal public void internalPutTarget(io.objectbox.Cursor<TARGET> targetCursor)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object