com.jenkov.mrpersister.itf.mapping
Interface IObjectMapping

All Known Implementing Classes:
ObjectMapping

public interface IObjectMapping

Represents one method from an object to the database, and/or from the database to the object. Both way mappings possible in the same object method.

There can only be one method method containing a given database column name, and only one method method containing any given object method. While method "overloading" would seem nice, meaning that you can have many mappings from different query/database fields to the same object methods all gathered in one object method, this cannot be done for the following reasons:

  1. With many different database column names in the object method it can be hard to know which method mappings are used when.
  2. Also, you risk getting into situations where the query result actually contains a column name that one of you method mappings are pointing too, in a situation where you don't want that column mapped to that method in the object.


  3. While the above problems could have been solved using some sort of method method "shadowing" or aliasing ignoring certain fields or having them mapped to other names, this has not (yet) been implemented. Also, using this kind of shadowing and aliasing could cause trouble all over the place if the original object method that these shadowings and aliases was based on, is changed. Instead of shadowing and aliasing the focus has been to make it easy to create new object mappings from scratch.

    Author:
    Jakob Jenkov, Jenkov Development

    Method Summary
     void addGetterMapping(IGetterMapping mapping)
              Adds a getter method method to this object method.
     void addSetterMapping(ISetterMapping mapping)
              Adds a getter method method to this object method.
     IGetterMapping getGetterMapping(java.lang.reflect.Method objectMethod)
              Returns the getter method method that is mapped to the object method passed as parameter objectMethod.
     IGetterMapping getGetterMapping(java.lang.String columnName)
              Returns the getter method method that is mapped to the database column name passed in parameter columnName.
     java.util.Collection getGetterMappings()
              Returns the method mappings that are mapped to getter methods on the mapped object class.
     java.lang.Class getObjectClass()
              Returns the class that this object method maps.
     IKey getPrimaryKey()
              Returns the current primary key mapping.
     java.lang.String getPrimaryKeyColumnName()
              Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.
     IGetterMapping getPrimaryKeyGetterMapping()
              Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.
     ISetterMapping getPrimaryKeySetterMapping()
              Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.
     IKeyValue getPrimaryKeyValueForObject(java.lang.Object object, IKeyValue keyValue)
              Returns an IKeyValue instance matching the primary key of this object mapping, and with the key values extracted from the given object.
     IKeyValue getPrimaryKeyValueForRecord(java.sql.ResultSet result, IKeyValue keyValue)
              Returns the primary key value as an IKeyValue instance for the record the ResultSet is pointing to.
     ISetterMapping getSetterMapping(java.lang.reflect.Method objectMethod)
              Returns the setter method method that is mapped to the object method passed as parameter objectMethod.
     ISetterMapping getSetterMapping(java.lang.String columnName)
              Returns the setter method method that is mapped to the database column name passed in parameter columnName.
     java.util.Collection getSetterMappings()
              Returns the method mappings that are mapped to setter methods on the mapped object class.
     java.lang.String getTableName()
              Returns the name of the database table that this object method maps to.
     boolean hasAutoGeneratedKeys()
              Returns true if this object mapping maps to auto generated keys.
     void removeGetterMapping(java.lang.reflect.Method method)
              Removes the getter method method that maps from the given Method to some database column.
     void removeGetterMapping(java.lang.String columnName)
              Removes the getter method method that maps to the provided database column name (column in table).
     void removeSetterMapping(java.lang.reflect.Method method)
              Removes the setter method method that maps from the given Method to some database column.
     void removeSetterMapping(java.lang.String columnName)
              Removes the setter method method that maps to the given database column name (column in table).
     void setObjectClass(java.lang.Class type)
              Sets the class that this object method maps.
     void setPrimaryKey(IKey key)
              Sets the current primary key mapping.
     void setPrimaryKeyColumnName(java.lang.String columnName)
              Deprecated. Use the setPrimaryKey() instead. The IKey interface supports compound keys.
     void setTableName(java.lang.String tableName)
              Sets the name of the database table that this object method maps to.
     

    Method Detail

    getTableName

    java.lang.String getTableName()
    Returns the name of the database table that this object method maps to.

    Returns:
    The database table name mapped to.

    setTableName

    void setTableName(java.lang.String tableName)
    Sets the name of the database table that this object method maps to.

    Parameters:
    tableName - The database table name to map to.

    getObjectClass

    java.lang.Class getObjectClass()
    Returns the class that this object method maps.

    Returns:
    The class mapped by this object method.

    setObjectClass

    void setObjectClass(java.lang.Class type)
    Sets the class that this object method maps.

    Parameters:
    type - The class mapped by this object method.

    hasAutoGeneratedKeys

    boolean hasAutoGeneratedKeys()
    Returns true if this object mapping maps to auto generated keys. Note: It only returns true if a column of the primary key is auto generated. Non-key columns are not taken into account.

    Returns:
    True if any column of the primary key is auto generated. False if not.

    getPrimaryKeyValueForObject

    IKeyValue getPrimaryKeyValueForObject(java.lang.Object object,
                                          IKeyValue keyValue)
                                          throws PersistenceException
    Returns an IKeyValue instance matching the primary key of this object mapping, and with the key values extracted from the given object. The object must be of the same class as the class this object mapping maps to (the class returned from getObjectClass()).

    Parameters:
    object - The object containing the primary key values to extract into the IKeyValue instance.
    Returns:
    An IKeyValue matching the IKey primary key of this object mapping, with the values extracted from this object.
    Throws:
    PersistenceException

    getPrimaryKeyValueForRecord

    IKeyValue getPrimaryKeyValueForRecord(java.sql.ResultSet result,
                                          IKeyValue keyValue)
                                          throws PersistenceException
    Returns the primary key value as an IKeyValue instance for the record the ResultSet is pointing to.

    Parameters:
    result -
    Returns:
    Throws:
    PersistenceException

    getPrimaryKey

    IKey getPrimaryKey()
    Returns the current primary key mapping. This IKey instance replaces the previous primary key methods getPrimaryKeyColumn(), setPrimaryKeyColumn(...), getPrimaryKeyGetterMapping() and getPrimaryKeySetterMapping() . This new key mapping object supports compound keys, which the previous methods did not.

    Returns:
    The current primary key mapping.

    setPrimaryKey

    void setPrimaryKey(IKey key)
    Sets the current primary key mapping. This IKey instance replaces the previous primary key methods getPrimaryKeyColumn(), setPrimaryKeyColumn(...), getPrimaryKeyGetterMapping() and getPrimaryKeySetterMapping() . This new key mapping object supports compound keys, which the previous methods did not.

    Parameters:
    key - The key mapping to use as the primary key mapping.

    getPrimaryKeyColumnName

    java.lang.String getPrimaryKeyColumnName()
    Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.

    Returns the database column name of the primary key for the table that this object method maps to, if any. If this object method doesn't map directly to a table but to some query instead, there may be no primary key for the object method. In that case, null is returned.

    Returns:
    The name of the primary key column for this object method. Null if no primary key is set.

    setPrimaryKeyColumnName

    void setPrimaryKeyColumnName(java.lang.String columnName)
    Deprecated. Use the setPrimaryKey() instead. The IKey interface supports compound keys.

    Sets the database column name of the primary key for the table that this object method maps to, if any. If this object method doesn't map directly to a table but to some query instead, there may be no primary key for the object method. In that case, set it to null (or don't set it at all. Defaults to null).

    Parameters:
    columnName - The name of the primary key column in the table that this object method maps to.

    getPrimaryKeyGetterMapping

    IGetterMapping getPrimaryKeyGetterMapping()
    Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.

    Returns the method method (if any) that maps to the primary key of the table mapped to.

    Returns:
    The method method of the primary key.

    getPrimaryKeySetterMapping

    ISetterMapping getPrimaryKeySetterMapping()
    Deprecated. Use the getPrimaryKey() instead. The IKey interface supports compound keys.

    Returns the method method (if any) that maps to the primary key of the table mapped to.

    Returns:
    The method method of the primary key.

    addGetterMapping

    void addGetterMapping(IGetterMapping mapping)
                          throws PersistenceException
    Adds a getter method method to this object method.

    Parameters:
    mapping - The method method to add.
    Throws:
    PersistenceException - If the given method method is not valid (is not null, and does at least contain object method and column name);

    addSetterMapping

    void addSetterMapping(ISetterMapping mapping)
                          throws PersistenceException
    Adds a getter method method to this object method.

    Parameters:
    mapping - The method method to add.
    Throws:
    PersistenceException - If the given method method is not valid (is not null, and does at least contain object method and column name);

    removeGetterMapping

    void removeGetterMapping(java.lang.String columnName)
    Removes the getter method method that maps to the provided database column name (column in table).

    Parameters:
    columnName - The database column name of the method method to remove.

    removeGetterMapping

    void removeGetterMapping(java.lang.reflect.Method method)
    Removes the getter method method that maps from the given Method to some database column.

    Parameters:
    method - The method mapped from of the method method to remove.

    removeSetterMapping

    void removeSetterMapping(java.lang.String columnName)
    Removes the setter method method that maps to the given database column name (column in table).

    Parameters:
    columnName - The database column name of the setter method method to remove.

    removeSetterMapping

    void removeSetterMapping(java.lang.reflect.Method method)
    Removes the setter method method that maps from the given Method to some database column.

    Parameters:
    method - The method mapped from of the method method to remove.

    getGetterMappings

    java.util.Collection getGetterMappings()
    Returns the method mappings that are mapped to getter methods on the mapped object class.

    Returns:
    A Collection of getter method mappings.

    getSetterMappings

    java.util.Collection getSetterMappings()
    Returns the method mappings that are mapped to setter methods on the mapped object class.

    Returns:
    A Collection of setter method mappings.

    getGetterMapping

    IGetterMapping getGetterMapping(java.lang.String columnName)
    Returns the getter method method that is mapped to the database column name passed in parameter columnName. If no getter method method is found with that database column name, null is returned.

    Parameters:
    columnName - The database column name to get the getter method method for.
    Returns:
    The getter method method matching the given database column name. Null if no matching getter method method is found.

    getGetterMapping

    IGetterMapping getGetterMapping(java.lang.reflect.Method objectMethod)
    Returns the getter method method that is mapped to the object method passed as parameter objectMethod. If no getter method method is found matching that Method, null is returned.

    Parameters:
    objectMethod - The method to get the getter method method for.
    Returns:
    The getter method method matching the given method. Null if no matching getter method method is found.

    getSetterMapping

    ISetterMapping getSetterMapping(java.lang.String columnName)
    Returns the setter method method that is mapped to the database column name passed in parameter columnName. If no setter method method is found with that database column name, null is returned.

    Parameters:
    columnName - The database column name to get the setter method method for.
    Returns:
    The setter method method matching the given database column name. Null if no matching setter method method is found.

    getSetterMapping

    ISetterMapping getSetterMapping(java.lang.reflect.Method objectMethod)
    Returns the setter method method that is mapped to the object method passed as parameter objectMethod. If no setter method method is found matching that Method, null is returned.

    Parameters:
    objectMethod - The method to get the setter method method for.
    Returns:
    The setter method method matching the given method. Null if no matching setter method method is found.