Package io.jmix.core

Class EntityStates

java.lang.Object
io.jmix.core.EntityStates

@Component("core_EntityStates") public class EntityStates extends Object
Provides information about entities states.
  • Field Details

  • Constructor Details

    • EntityStates

      public EntityStates()
  • Method Details

    • isNew

      public boolean isNew(Object entity)
      Determines whether the instance is New, i.e. just created and not stored in database yet.
      Parameters:
      entity - entity instance
      Returns:
      - true if the instance is a new JPA entity, or if it is actually in Managed state but newly-persisted in this transaction
      - true if the instance is not a JPA entity
      - false otherwise
      Throws:
      IllegalArgumentException - if entity instance is null
    • isManaged

      public boolean isManaged(Object entity)
      Determines whether the instance is Managed, i.e. attached to a persistence context.
      Parameters:
      entity - entity instance
      Returns:
      - true if the instance is managed,
      - false if it is New (and not yet persisted) or Detached, or if it is not a JPA entity
      Throws:
      IllegalArgumentException - if entity instance is null
    • isDetached

      public boolean isDetached(Object entity)
      Determines whether the instance is Detached, i.e. stored in database but not attached to a persistence context at the moment.
      Parameters:
      entity - entity instance
      Returns:
      - true if the instance is detached,
      - false if it is New or Managed, or if it is not a JPA entity
      Throws:
      IllegalArgumentException - if entity instance is null
    • isLoaded

      public boolean isLoaded(Object entity, String property)
      Checks if the property is loaded from DB.

      Non-persistent attributes are considered loaded if they do not have related properties, or all related properties are loaded.

      Parameters:
      entity - entity
      property - name of the property. Only immediate attributes of the entity are supported.
      Returns:
      true if loaded
    • checkLoaded

      public void checkLoaded(Object entity, String... properties)
      Check that entity has all specified properties loaded from DB. Throw exception if property is not loaded.
      Parameters:
      entity - entity
      properties - property names
      Throws:
      IllegalArgumentException - if at least one of properties is not loaded
    • checkLoadedWithFetchPlan

      protected void checkLoadedWithFetchPlan(Object entity, FetchPlan fetchPlan, Set visited)
    • checkLoadedWithFetchPlan

      public void checkLoadedWithFetchPlan(Object entity, FetchPlan fetchPlan)
      Check that all properties of the fetch plan are loaded from DB for the passed entity. Throws exception if some property is not loaded.
      Parameters:
      entity - entity
      fetchPlan - fetch plan
      Throws:
      IllegalArgumentException - if at least one of properties is not loaded
    • checkLoadedWithFetchPlan

      public void checkLoadedWithFetchPlan(Object entity, String fetchPlanName)
      Check that all properties of the fetch plan are loaded from DB for the passed entity. Throws exception if some property is not loaded.
      Parameters:
      entity - entity
      fetchPlanName - fetch plan name
      Throws:
      IllegalArgumentException - if at least one of properties is not loaded
    • isLoadedWithFetchPlan

      protected boolean isLoadedWithFetchPlan(Object entity, FetchPlan fetchPlan, Set visited)
    • isLoadedWithFetchPlan

      public boolean isLoadedWithFetchPlan(Object entity, FetchPlan fetchPlan)
      Check that all properties of the fetch plan are loaded from DB for the passed entity.
      Parameters:
      entity - entity
      fetchPlan - fetch plan
      Returns:
      false if at least one of properties is not loaded
    • isLoadedWithFetchPlan

      public boolean isLoadedWithFetchPlan(Object entity, String fetchPlanName)
      Check that all properties of the fetch plan are loaded from DB for the passed entity.
      Parameters:
      entity - entity
      fetchPlanName - fetch plan name
      Returns:
      false if at least one of properties is not loaded
    • getCurrentFetchPlan

      public FetchPlan getCurrentFetchPlan(Object entity)
      Returns a fetch plan that corresponds to the loaded attributes of the given entity instance.
      Parameters:
      entity - entity instance
      Returns:
      fetch plan
    • recursivelyConstructCurrentFetchPlan

      protected void recursivelyConstructCurrentFetchPlan(Entity entity, FetchPlanBuilder builder, HashSet<Object> visited)
    • isDeleted

      public boolean isDeleted(Object entity)
      Determines whether the entity instance was deleted.
      Parameters:
      entity - entity instance
      Returns:
      - true if the instance was deleted - false otherwise
      Throws:
      IllegalArgumentException - if entity instance is null
    • setNew

      public void setNew(Object entity, boolean isNew)
      Manages the New state of the entity instance.
      See Also:
    • makeDetached

      public void makeDetached(Object entity)
      Makes a newly constructed object detached. The detached object can be passed to DataManager.save() or to EntityManager.merge() to save its state to the database.

      If an object with such ID does not exist in the database, a new object will be inserted.

      If the entity is Versioned, the version attribute should be equal to the latest version existing in the database, or null for a new object.

      Parameters:
      entity - entity in the New state
      Throws:
      IllegalStateException - if the entity is Managed
      See Also:
    • makePatch

      public void makePatch(Object entity)
      Makes a newly constructed object a patch object. The patch object is !isNew() && !isDetached() && !isManaged(). The patch object can be passed to DataManager.commit() or to EntityManager.merge() to save its state to the database. Only non-null values of attributes are updated.

      If an object with such ID does not exist in the database, a new object will be inserted.

      If the entity is Versioned, the version attribute should be null or equal to the latest version existing in the database.

      Parameters:
      entity - entity in the New or Detached state
      Throws:
      IllegalStateException - if the entity is Managed
      See Also: