Class RelationshipManagerImpl

java.lang.Object
org.datanucleus.state.RelationshipManagerImpl
All Implemented Interfaces:
RelationshipManager

public class RelationshipManagerImpl extends Object implements RelationshipManager
Manager for (bidirectional) relationships of an object (StateManager). Designed as a stand-alone process to run just before flush. Updates on bidirectional relations are registered during the persistence process. Call to checkConsistency() will check for consistency and throw exceptions as appropriate. Call to process() will perform updates at the other side of the registered relations so all is consistent.
  • Field Details

  • Constructor Details

    • RelationshipManagerImpl

      public RelationshipManagerImpl(DNStateManager sm)
      Constructor.
      Parameters:
      sm - StateManager for the object that we are managing relations for.
  • Method Details

    • clearFields

      public void clearFields()
      Description copied from interface: RelationshipManager
      Convenience method to clear all fields from being managed.
      Specified by:
      clearFields in interface RelationshipManager
    • relationChange

      public void relationChange(int fieldNumber, Object oldValue, Object newValue)
      Description copied from interface: RelationshipManager
      Method that is called when the user calls setXXX() on a field.
      Specified by:
      relationChange in interface RelationshipManager
      Parameters:
      fieldNumber - Number of the field
      oldValue - The old value
      newValue - The new value
    • relationAdd

      public void relationAdd(int fieldNumber, Object val)
      Description copied from interface: RelationshipManager
      Method to register a change in the contents of a container field, with an object being added.
      Specified by:
      relationAdd in interface RelationshipManager
      Parameters:
      fieldNumber - Number of the field
      val - Value being added
    • relationRemove

      public void relationRemove(int fieldNumber, Object val)
      Description copied from interface: RelationshipManager
      Method to register a change in the contents of a container field, with an object being removed.
      Specified by:
      relationRemove in interface RelationshipManager
      Parameters:
      fieldNumber - Number of the field
      val - Value being removed
    • managesField

      public boolean managesField(int fieldNumber)
      Accessor for whether a field is being managed.
      Specified by:
      managesField in interface RelationshipManager
      Parameters:
      fieldNumber - Number of the field
      Returns:
      Whether it is currently managed
    • checkConsistency

      public void checkConsistency()
      Description copied from interface: RelationshipManager
      Method to check for consistency the managed relations of this object with the related objects.
      Specified by:
      checkConsistency in interface RelationshipManager
    • process

      public void process()
      Description copied from interface: RelationshipManager
      Method to process the (bidirectional) relations for this object.
      Specified by:
      process in interface RelationshipManager
    • checkOneToOneBidirectionalRelation

      protected void checkOneToOneBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to check the consistency of the passed field as 1-1. Processes the case where we had a 1-1 field set at this side previously to some value and now to some other value. We need to make sure that all of the affected objects are now related consistently. Taking an example
      a.b = b1; a.b = b2;
      so A's b field is changed from b1 to b2. The following changes are likely to be necessary
      • b1.a = null - so we null out the old related objects link back to this object
      • b2.oldA = null - if b2 was previously related to a different A, null out that objects link to b2
      • b2.a = a - set the link from b2 back to a so it is bidirectional
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the field
    • checkOneToManyBidirectionalRelation

      protected void checkOneToManyBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to check the consistency of the passed field as 1-N.
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the collection
    • checkManyToOneBidirectionalRelation

      protected void checkManyToOneBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to check the consistency of the passed field as N-1. Processes the case where we had an N-1 field set at this side previously to some value and now to some other value.That is, this object was in some collection/map originally, and now should be in some other collection/map. So in terms of an example this object "a" was in collection "b1.as" before and is now in "b2.as". The following changes are likely to be necessary
      • b1.getAs().remove(a) - remove it from b1.as if still present
      • b2.getAs().add(a) - add it to b1.as if not present
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the field
    • checkManyToManyBidirectionalRelation

      protected void checkManyToManyBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to check consistency of the passed field as M-N.
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the collection
    • processOneToOneBidirectionalRelation

      protected void processOneToOneBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to process all 1-1 bidir fields. Processes the case where we had a 1-1 field set at this side previously to some value and now to some other value. We need to make sure that all of the affected objects are now related consistently. Taking an example
      a.b = b1; a.b = b2;
      so A's b field is changed from b1 to b2. The following changes are likely to be necessary
      • b1.a = null - so we null out the old related objects link back to this object
      • b2.oldA = null - if b2 was previously related to a different A, null out that objects link to b2
      • b2.a = a - set the link from b2 back to a so it is bidirectional
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the field
    • processOneToManyBidirectionalRelation

      protected void processOneToManyBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to process all 1-N bidirectional fields.
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the collection
    • processManyToOneBidirectionalRelation

      protected void processManyToOneBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to process all N-1 bidirectional fields. Processes the case where we had an N-1 field set at this side previously to some value and now to some other value.That is, this object was in some collection/map originally, and now should be in some other collection/map. So in terms of an example this object "a" was in collection "b1.as" before and is now in "b2.as". The following changes are likely to be necessary
      • b1.getAs().remove(a) - remove it from b1.as if still present
      • b2.getAs().add(a) - add it to b1.as if not present
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the collection
    • processManyToManyBidirectionalRelation

      protected void processManyToManyBidirectionalRelation(AbstractMemberMetaData mmd, ClassLoaderResolver clr, ExecutionContext ec, List<RelationshipManagerImpl.RelationChange> changes)
      Method to process all M-N bidirectional fields.
      Parameters:
      mmd - MetaData for the field
      clr - ClassLoader resolver
      ec - ExecutionContext
      changes - List of changes to the collection