Class ModelUtils


  • public class ModelUtils
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addToCollectionField​(Field field, Object collectionOwnerInstance, Object elementToAdd)  
      static <T> Collection<T> getAsCollection​(Object value)
      Return the given object as a collection.
      static Class<?> getCollectionType​(Field field)
      Get the type of elements in a collection field.
      static org.springframework.data.relational.core.sql.Condition getConditionOnId​(SqlQuery<?> query, org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static org.springframework.data.relational.core.sql.Condition getConditionOnProperties​(SqlQuery<?> query, org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static Object getDatabaseValue​(Object instance, org.springframework.data.relational.core.mapping.RelationalPersistentProperty property, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static Field getForeignTableFieldForJoinKey​(Class<?> entity, String joinKey, Class<?> targetType)
      Return the foreign table field on the given entity type, having the given join key.
      static Field getForeignTableFieldForProperty​(Class<?> entity, String propertyName)
      Return the foreign table field on the given property in the given entity type.
      static ForeignTable getForeignTableForProperty​(Class<?> entity, String propertyName)
      Return the foreign table field on the given property in the given entity type.
      static Collection<org.springframework.data.util.Pair<Field,​ForeignTable>> getForeignTables​(Class<?> entity)
      Return the list of foreign tables declared in the given entity type.
      static org.springframework.data.util.Pair<Field,​ForeignTable> getForeignTableWithFieldForJoinKey​(Class<?> entity, String joinKey, Class<?> targetType)
      Return the foreign table field on the given entity type, having the given join key.
      static Object getId​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, PropertiesSource source)  
      static Object getId​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static CompositeIdValue getIdFromProperties​(Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties, PropertiesSource source)  
      static CompositeIdValue getIdFromProperties​(Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static Object getIdPropertyValue​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, PropertiesSource source)  
      static Object getIdPropertyValue​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor)  
      static Object getPersistedDatabaseValue​(EntityState state, org.springframework.data.relational.core.mapping.RelationalPersistentProperty property, org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)  
      static List<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> getProperties​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, String... names)  
      static Class<?> getRequiredCollectionType​(Field field)
      Get the type of elements in a collection field.
      static Field getRequiredForeignTableFieldForJoinKey​(Class<?> entity, String joinKey, Class<?> targetType)
      Return the foreign table field on the given entity type, having the given join key.
      static Field getRequiredForeignTableFieldForProperty​(Class<?> entity, String propertyName)
      Return the foreign table field on the given property in the given entity type.
      static ForeignTable getRequiredForeignTableForProperty​(Class<?> entity, String propertyName)
      Return the foreign table field on the given property in the given entity type.
      static org.springframework.data.util.Pair<Field,​ForeignTable> getRequiredForeignTableWithFieldForJoinKey​(Class<?> entity, String joinKey, Class<?> targetType)
      Return the foreign table field on the given entity type, having the given join key.
      static Object getRequiredId​(Object instance, org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType, org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor)
      Return the identifier for the given entity.
      static boolean isCollection​(Field field)
      Check if the given field is a collection.
      static boolean isCollectionType​(Class<?> type)
      Check if the given type is a collection.
      static boolean isForeignTableField​(Field field)
      Return true if the given field is associated to a @ForeignTable annotation.
      static boolean isNullable​(org.springframework.data.relational.core.mapping.RelationalPersistentProperty property)
      Check if a property may be null.
      It cannot be null if: the type is a primitive type this is the id property this is a foreign key, and it is specified as non optional this is not a foreign key, and the column definition specifies the column as nullable
      static boolean isUpdatable​(org.springframework.data.relational.core.mapping.RelationalPersistentProperty property)
      Check if a property may be updated.
      static void removeFromCollectionField​(Field field, Object collectionOwnerInstance, Object elementToRemove)  
      static void setReverseLink​(Object instance, Object linkedInstance, org.springframework.data.relational.core.mapping.RelationalPersistentProperty linkedProperty)
      Set the foreign table field on the given instance to the given linkedInstance.
    • Method Detail

      • isNullable

        public static boolean isNullable​(org.springframework.data.relational.core.mapping.RelationalPersistentProperty property)
        Check if a property may be null.
        It cannot be null if:
        • the type is a primitive type
        • this is the id property
        • this is a foreign key, and it is specified as non optional
        • this is not a foreign key, and the column definition specifies the column as nullable
        Parameters:
        property -
        Returns:
      • isUpdatable

        public static boolean isUpdatable​(org.springframework.data.relational.core.mapping.RelationalPersistentProperty property)
        Check if a property may be updated.
        Parameters:
        property -
        Returns:
      • setReverseLink

        public static void setReverseLink​(Object instance,
                                          Object linkedInstance,
                                          org.springframework.data.relational.core.mapping.RelationalPersistentProperty linkedProperty)
        Set the foreign table field on the given instance to the given linkedInstance.
        Parameters:
        instance - entity having the foreign table field
        linkedInstance - entity having the foreign key
        linkedProperty - foreign key property
      • getForeignTableFieldForJoinKey

        @Nullable
        public static Field getForeignTableFieldForJoinKey​(Class<?> entity,
                                                           String joinKey,
                                                           Class<?> targetType)
        Return the foreign table field on the given entity type, having the given join key.
        Parameters:
        entity - entity type on which to search for the foreign table field
        joinKey - join key
        Returns:
        the field
      • getRequiredForeignTableFieldForJoinKey

        public static Field getRequiredForeignTableFieldForJoinKey​(Class<?> entity,
                                                                   String joinKey,
                                                                   Class<?> targetType)
        Return the foreign table field on the given entity type, having the given join key.
        Parameters:
        entity - entity type on which to search for the foreign table field
        joinKey - join key
        Returns:
        the field
      • getForeignTableWithFieldForJoinKey

        @Nullable
        public static org.springframework.data.util.Pair<Field,​ForeignTable> getForeignTableWithFieldForJoinKey​(Class<?> entity,
                                                                                                                      String joinKey,
                                                                                                                      Class<?> targetType)
        Return the foreign table field on the given entity type, having the given join key.
        Parameters:
        entity - entity type on which to search for the foreign table field
        joinKey - join key
        Returns:
        the field and the foreign table annotation
      • getRequiredForeignTableWithFieldForJoinKey

        public static org.springframework.data.util.Pair<Field,​ForeignTable> getRequiredForeignTableWithFieldForJoinKey​(Class<?> entity,
                                                                                                                              String joinKey,
                                                                                                                              Class<?> targetType)
        Return the foreign table field on the given entity type, having the given join key.
        Parameters:
        entity - entity type on which to search for the foreign table field
        joinKey - join key
        Returns:
        the field and the foreign table annotation
      • getForeignTableFieldForProperty

        @Nullable
        public static Field getForeignTableFieldForProperty​(Class<?> entity,
                                                            String propertyName)
        Return the foreign table field on the given property in the given entity type.
        Parameters:
        entity - entity type on which to search for the foreign table field
        propertyName - foreign table property
        Returns:
        the field
      • getRequiredForeignTableFieldForProperty

        public static Field getRequiredForeignTableFieldForProperty​(Class<?> entity,
                                                                    String propertyName)
        Return the foreign table field on the given property in the given entity type.
        Parameters:
        entity - entity type on which to search for the foreign table field
        propertyName - foreign table property
        Returns:
        the field
      • getForeignTableForProperty

        @Nullable
        public static ForeignTable getForeignTableForProperty​(Class<?> entity,
                                                              String propertyName)
        Return the foreign table field on the given property in the given entity type.
        Parameters:
        entity - entity type on which to search for the foreign table field
        propertyName - foreign table property
        Returns:
        the foreign table annotation
      • getRequiredForeignTableForProperty

        public static ForeignTable getRequiredForeignTableForProperty​(Class<?> entity,
                                                                      String propertyName)
        Return the foreign table field on the given property in the given entity type.
        Parameters:
        entity - entity type on which to search for the foreign table field
        propertyName - foreign table property
        Returns:
        the foreign table annotation
      • isForeignTableField

        public static boolean isForeignTableField​(Field field)
        Return true if the given field is associated to a @ForeignTable annotation.
        Parameters:
        field - field
        Returns:
        true if it is a foreign table
      • getForeignTables

        public static Collection<org.springframework.data.util.Pair<Field,​ForeignTable>> getForeignTables​(Class<?> entity)
        Return the list of foreign tables declared in the given entity type.
        Parameters:
        entity - entity type
        Returns:
        list of fields with their corresponding foreign table annotation
      • getRequiredId

        public static Object getRequiredId​(Object instance,
                                           org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                           @Nullable
                                           org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor)
        Return the identifier for the given entity.
        Parameters:
        instance - entity
        entityType - entity type
        Returns:
        identifier
      • isCollection

        public static boolean isCollection​(Field field)
        Check if the given field is a collection.
        Parameters:
        field - field to check
        Returns:
        true if the field is an array or a Collection
      • isCollectionType

        public static boolean isCollectionType​(Class<?> type)
        Check if the given type is a collection.
        Parameters:
        type - type to check
        Returns:
        true if the type is an array or implements Collection
      • getAsCollection

        @Nullable
        public static <T> Collection<T> getAsCollection​(Object value)
        Return the given object as a collection.
        Parameters:
        value - the object
        Returns:
        a collection or null
      • getCollectionType

        @Nullable
        public static Class<?> getCollectionType​(Field field)
        Get the type of elements in a collection field.
        Parameters:
        field - field
        Returns:
        type of elements
      • getRequiredCollectionType

        public static Class<?> getRequiredCollectionType​(Field field)
        Get the type of elements in a collection field.
        Parameters:
        field - field
        Returns:
        type of elements
      • getDatabaseValue

        public static Object getDatabaseValue​(Object instance,
                                              org.springframework.data.relational.core.mapping.RelationalPersistentProperty property,
                                              org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)
      • getPersistedDatabaseValue

        public static Object getPersistedDatabaseValue​(EntityState state,
                                                       org.springframework.data.relational.core.mapping.RelationalPersistentProperty property,
                                                       org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)
      • getProperties

        public static List<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> getProperties​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                                                                                                        String... names)
      • getId

        public static Object getId​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                   org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor,
                                   org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)
      • getIdPropertyValue

        public static Object getIdPropertyValue​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                                org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor)
      • getIdFromProperties

        public static CompositeIdValue getIdFromProperties​(Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties,
                                                           org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor,
                                                           org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)
      • getId

        public static Object getId​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                   PropertiesSource source)
      • getIdPropertyValue

        public static Object getIdPropertyValue​(org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                                PropertiesSource source)
      • getIdFromProperties

        public static CompositeIdValue getIdFromProperties​(Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties,
                                                           PropertiesSource source)
      • getConditionOnId

        public static org.springframework.data.relational.core.sql.Condition getConditionOnId​(SqlQuery<?> query,
                                                                                              org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                                                                              org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor,
                                                                                              org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)
      • getConditionOnProperties

        public static org.springframework.data.relational.core.sql.Condition getConditionOnProperties​(SqlQuery<?> query,
                                                                                                      org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?> entityType,
                                                                                                      Iterable<org.springframework.data.relational.core.mapping.RelationalPersistentProperty> properties,
                                                                                                      org.springframework.data.mapping.PersistentPropertyAccessor<?> accessor,
                                                                                                      org.springframework.data.mapping.context.MappingContext<org.springframework.data.relational.core.mapping.RelationalPersistentEntity<?>,​? extends org.springframework.data.relational.core.mapping.RelationalPersistentProperty> mappingContext)