Class ToOne<TARGET>

  • Type Parameters:
    TARGET - target object type (@Entity class).
    All Implemented Interfaces:
    java.io.Serializable

    public class ToOne<TARGET>
    extends java.lang.Object
    implements java.io.Serializable
    A to-one relation of an entity that references one object of a ToOne entity.

    Example:

    {@code
     // Java
    See Also:
    Serialized Form
    • 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 or null if there is none.

        ToOne uses 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.
      • 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. Pass 0 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(TARGET)
      • setTarget

        public void setTarget​(@Nullable
                              TARGET target)
        Prepares to set the target object of this relation. Pass null 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:
        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.

      • getTargetId

        public long getTargetId()
      • internalRequiresPutTarget

        @Internal
        public boolean internalRequiresPutTarget()
      • internalPutTarget

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object