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
    • Field Detail

      • entity

        private final java.lang.Object entity
      • virtualProperty

        private final boolean virtualProperty
      • boxStore

        private transient BoxStore boxStore
      • entityBox

        private transient Box<java.lang.Object> entityBox
      • targetBox

        private transient volatile Box<TARGET> targetBox
      • 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 to getTargetId(), 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 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.
      • 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. 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)
      • setAndUpdateTargetId

        void setAndUpdateTargetId​(long targetId)
      • 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.

      • 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:
        equals in class java.lang.Object
      • hashCode

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