com.jenkov.mrpersister.itf.mapping
Interface IKey

All Known Implementing Classes:
Key

public interface IKey

This interface represents a mapping of a primary or foreing key in a database, to a list of getter and setter mappings. This key mapping is a replacement for the previous primary key mappings in the IObjectMapping interface. The previous primary key mapping constructs did not support compound keys (multi-column keys). The IKey supports that. In addition a IKey can be used to map foreign keys too.

A key mapping consists of a table name and collections of getter and setter methods that are mapped to the columns included in the key. For a primary key the table name is the same table as the key exists in. For a foreign key the table name is the table the primary key the foreign key points to exists in.

Author:
Jakob Jenkov - Copyright 2005 Jenkov Development

Method Summary
 void addColumn(java.lang.String column)
          Adds a column to this key mapping.
 java.lang.String getColumn()
          If this key only consists of one column, this method will return that column.
 java.util.Collection getColumns()
          Returns the collection of columns used in this key mapping.
 java.lang.String getTable()
          Returns the table this key mapping points to.
 boolean isValid(IKeyValue keyValue)
          Validates an IKeyValue instance against this key.
 void removeColumn(java.lang.String column)
          Removes a column from this key mapping
 void setColumns(java.util.Collection columns)
          Sets the collection of columns to use in this key mapping.
 void setTable(java.lang.String table)
          Sets the table this key points to.
 int size()
          Returns the number of columns in this key.
 IKeyValue toKeyValue(java.lang.Object keyValueObject)
          Wraps the given key value object in a IKeyValue instance.
 

Method Detail

getTable

java.lang.String getTable()
Returns the table this key mapping points to. For a primary key this is the same table as the key exists in. For a foreign key this is the table the primary key the foreign key points to exists in.

Returns:
The table this key mapping points to.

setTable

void setTable(java.lang.String table)
Sets the table this key points to. For a primary key this is the same table as the key exists in. For a foreign key this is the table the primary key the foreign key points to exists in.

Parameters:
table - The table this key mapping is to point to.

addColumn

void addColumn(java.lang.String column)
Adds a column to this key mapping.

Parameters:
column - The column to add to this key mapping.

removeColumn

void removeColumn(java.lang.String column)
Removes a column from this key mapping

Parameters:
column - The column to remove

getColumns

java.util.Collection getColumns()
Returns the collection of columns used in this key mapping.

Returns:
The collection of columns used in this key mapping.

setColumns

void setColumns(java.util.Collection columns)
Sets the collection of columns to use in this key mapping.

Parameters:
columns - The collection of columns to use in this key mapping.

size

int size()
Returns the number of columns in this key.

Returns:
The number of columns in this key.

isValid

boolean isValid(IKeyValue keyValue)
Validates an IKeyValue instance against this key. If the key value contains values for all columns in this key, this method returns true. False if not.

If the key value contains values for columns that are not part of this key, these column values are ignored.

Parameters:
keyValue - The key value to validate.
Returns:
True if the key is valid. False if not.

getColumn

java.lang.String getColumn()
                           throws PersistenceException
If this key only consists of one column, this method will return that column. Otherwise a PersistenceException is thrown.

Returns:
The column of this key, if it consists of only one column.
Throws:
PersistenceException - If this key contains zero, or more than one column.

toKeyValue

IKeyValue toKeyValue(java.lang.Object keyValueObject)
                     throws PersistenceException
Wraps the given key value object in a IKeyValue instance. If the object to be wrapped is already a IKeyValue instance it will be returned unchanged. This will only work if this IKey instance only consists of a single column.

Parameters:
keyValueObject - The object to wrap in a IKeyValue instance.
Returns:
An IKeyValue instance wrapping the given object.
Throws:
PersistenceException - if this IKey instance is a compound key (multi column key), or if empty.