Package org.apache.openjpa.jdbc.schema
Class ForeignKey
- java.lang.Object
-
- org.apache.openjpa.jdbc.schema.Constraint
-
- org.apache.openjpa.jdbc.schema.ForeignKey
-
- All Implemented Interfaces:
java.io.Serializable
public class ForeignKey extends Constraint
Represents a database foreign key; may be a logical key with no database representation. This class can also represent a partial key, aligning withDatabaseMetaData
.- Author:
- Abe White
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ForeignKey.FKMapKey
-
Field Summary
Fields Modifier and Type Field Description static int
ACTION_CASCADE
Cascade any modification of the joined primary key columns to this table.static int
ACTION_DEFAULT
Set the local columns to their default values if the primary key columns are modified.static int
ACTION_NONE
Logical foreign key; links columns, but does not perform any action when the joined primary key columns are modified.static int
ACTION_NULL
Null the local columns if the joined primary key columns are modified.static int
ACTION_RESTRICT
Throw an exception if joined primary key columns are modified.
-
Constructor Summary
Constructors Constructor Description ForeignKey()
Default constructor.ForeignKey(java.lang.String name, Table table)
Deprecated.ForeignKey(DBIdentifier name, Table table)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addColumn(ForeignKey fk)
Joins the column of a single column FK to this FK.boolean
columnsMatch(Column[] fkCols, Column[] fkPKCols)
Return true if the given local and foreign columns match those on this key.boolean
containsColumn(Column col)
Return true if the fk includes the given local column.boolean
containsConstantColumn(Column col)
Return true if the fk includes the given local column.boolean
containsConstantPrimaryKeyColumn(Column col)
Return true if the fk includes the given primary key column.boolean
containsPrimaryKeyColumn(Column col)
Return true if the fk includes the given primary key column.void
deref()
void
derefColumns()
Deref all columns in this key.boolean
equals(java.lang.Object o)
boolean
equalsForeignKey(ForeignKey fk)
Foreign keys are equal if the satisfy the equality constraints ofConstraint
and they have the same local and primary key columns and action.static int
getAction(java.lang.String name)
Return the foreign key action constant for the given action name.static java.lang.String
getActionName(int action)
Return the foreign key action name for the given action constant.Column
getColumn(Column pk)
Return the local column joined to the given primary key column.Column[]
getColumns()
Return the local columns in the foreign key local table order.java.lang.Object
getConstant(Column local)
Return the constant value assigned to the given local column.Column[]
getConstantColumns()
Return the local columns that we link to using constant values.Column[]
getConstantPrimaryKeyColumns()
Return the primary key columns that we link to using constant values.java.lang.Object[]
getConstants()
Return the constant values assigned to the local columns returned bygetConstantColumns()
.int
getDeleteAction()
Return the delete action for the key.int
getIndex()
Return the foreign key's 0-based index in the owning table.int
getKeySequence()
The sequence of this join in the foreign key.Column
getPrimaryKeyColumn(Column local)
Return the primary key column joined to the given local column.DBIdentifier
getPrimaryKeyColumnIdentifier()
java.lang.String
getPrimaryKeyColumnName()
Deprecated.Column[]
getPrimaryKeyColumns()
Return the foreign columns in the foreign key, in join-order with the result ofgetColumns()
.java.lang.Object
getPrimaryKeyConstant(Column pk)
Return the constant value assigned to the given primary key column.java.lang.Object[]
getPrimaryKeyConstants()
Return the constant values assigned to the primary key columns returned bygetConstantPrimaryKeyColumns()
.DBIdentifier
getPrimaryKeySchemaIdentifier()
java.lang.String
getPrimaryKeySchemaName()
Deprecated.Table
getPrimaryKeyTable()
Returns the table this foreign key is linking to, if it is known yet.DBIdentifier
getPrimaryKeyTableIdentifier()
java.lang.String
getPrimaryKeyTableName()
Deprecated.int
getRefCount()
int
getUpdateAction()
Return the update action for the key.int
hashCode()
boolean
hasNotNullColumns()
Checks for non-nullable local columns.boolean
isLogical()
Return whether this constraint is a logical constraint only; i.e.boolean
isPrimaryKeyAutoAssigned()
Whether the primary key columns of this key are auto-incrementing, or whether they themselves are members of a foreign key who's primary key is auto-incrementing (recursing to arbitrary depth).void
join(Column local, Column toPK)
Join a local column to a primary key column of another table.void
joinConstant(java.lang.Object val, Column toPK)
Join a constant value to a primary key column of another table.void
joinConstant(Column col, java.lang.Object val)
Join a constant value to a local column of this table.DBIdentifier
loadIdentifierFromDB(DBDictionary dbdict, java.sql.Connection conn)
java.lang.String
loadNameFromDB(DBDictionary dbdict, java.sql.Connection conn)
Deprecated.void
ref()
void
refColumns()
Ref all columns in this key.boolean
removeJoin(Column col)
Remove any joins inolving the given column.void
setConstantJoins(java.lang.Object[] consts, Column[] pkCols)
Set the foreign key's constant joins.void
setConstantJoins(Column[] cols, java.lang.Object[] consts)
Set the foreign key's constant joins.void
setDeleteAction(int action)
Set the delete action for the key.void
setJoins(Column[] cols, Column[] pkCols)
Set the foreign key's joins.void
setKeySequence(int seq)
The sequence of this join in the foreign key.void
setPrimaryKeyColumnIdentifier(DBIdentifier pkColumnName)
void
setPrimaryKeyColumnName(java.lang.String pkColumnName)
Deprecated.void
setPrimaryKeySchemaIdentifier(DBIdentifier pkSchemaName)
void
setPrimaryKeySchemaName(java.lang.String pkSchemaName)
Deprecated.void
setPrimaryKeyTableIdentifier(DBIdentifier pkTableName)
void
setPrimaryKeyTableName(java.lang.String pkTableName)
Deprecated.void
setUpdateAction(int action)
Set the update action for the key.-
Methods inherited from class org.apache.openjpa.jdbc.schema.Constraint
getColumnIdentifier, getColumnName, getFullIdentifier, getFullName, getIdentifier, getName, getQualifiedPath, getSchemaIdentifier, getSchemaName, getTable, getTableIdentifier, getTableName, isDeferred, setColumnIdentifier, setColumnName, setDeferred, setIdentifier, setName, setSchemaIdentifier, setSchemaName, setTableIdentifier, setTableName, toString
-
-
-
-
Field Detail
-
ACTION_NONE
public static final int ACTION_NONE
Logical foreign key; links columns, but does not perform any action when the joined primary key columns are modified.- See Also:
- Constant Field Values
-
ACTION_RESTRICT
public static final int ACTION_RESTRICT
Throw an exception if joined primary key columns are modified.- See Also:
- Constant Field Values
-
ACTION_CASCADE
public static final int ACTION_CASCADE
Cascade any modification of the joined primary key columns to this table. If the joined primary key row is deleted, the row in this table will also be deleted.- See Also:
- Constant Field Values
-
ACTION_NULL
public static final int ACTION_NULL
Null the local columns if the joined primary key columns are modified.- See Also:
- Constant Field Values
-
ACTION_DEFAULT
public static final int ACTION_DEFAULT
Set the local columns to their default values if the primary key columns are modified.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ForeignKey
public ForeignKey()
Default constructor.
-
ForeignKey
@Deprecated public ForeignKey(java.lang.String name, Table table)
Deprecated.Constructor.- Parameters:
name
- the foreign key name, if anytable
- the local table of the foreign key
-
ForeignKey
public ForeignKey(DBIdentifier name, Table table)
-
-
Method Detail
-
getAction
public static int getAction(java.lang.String name)
Return the foreign key action constant for the given action name.
-
getActionName
public static java.lang.String getActionName(int action)
Return the foreign key action name for the given action constant.
-
isLogical
public boolean isLogical()
Description copied from class:Constraint
Return whether this constraint is a logical constraint only; i.e. if it does not exist in the database.- Specified by:
isLogical
in classConstraint
-
isPrimaryKeyAutoAssigned
public boolean isPrimaryKeyAutoAssigned()
Whether the primary key columns of this key are auto-incrementing, or whether they themselves are members of a foreign key who's primary key is auto-incrementing (recursing to arbitrary depth).
-
getPrimaryKeyTableName
@Deprecated public java.lang.String getPrimaryKeyTableName()
Deprecated.The name of the primary key table.
-
getPrimaryKeyTableIdentifier
public DBIdentifier getPrimaryKeyTableIdentifier()
-
setPrimaryKeyTableName
@Deprecated public void setPrimaryKeyTableName(java.lang.String pkTableName)
Deprecated.The name of the primary key table. You can only set the primary key table name on foreign keys that have not already been joined.
-
setPrimaryKeyTableIdentifier
public void setPrimaryKeyTableIdentifier(DBIdentifier pkTableName)
-
getPrimaryKeySchemaName
@Deprecated public java.lang.String getPrimaryKeySchemaName()
Deprecated.The name of the primary key table's schema.
-
getPrimaryKeySchemaIdentifier
public DBIdentifier getPrimaryKeySchemaIdentifier()
-
setPrimaryKeySchemaName
@Deprecated public void setPrimaryKeySchemaName(java.lang.String pkSchemaName)
Deprecated.The name of the primary key table's schema. You can only set the primary key schema name on foreign keys that have not already been joined.
-
setPrimaryKeySchemaIdentifier
public void setPrimaryKeySchemaIdentifier(DBIdentifier pkSchemaName)
-
getPrimaryKeyColumnName
@Deprecated public java.lang.String getPrimaryKeyColumnName()
Deprecated.The name of the primary key column.
-
getPrimaryKeyColumnIdentifier
public DBIdentifier getPrimaryKeyColumnIdentifier()
-
setPrimaryKeyColumnName
@Deprecated public void setPrimaryKeyColumnName(java.lang.String pkColumnName)
Deprecated.The name of the primary key column. You can only set the primary key column name on foreign keys that have not already been joined.
-
setPrimaryKeyColumnIdentifier
public void setPrimaryKeyColumnIdentifier(DBIdentifier pkColumnName)
-
getKeySequence
public int getKeySequence()
The sequence of this join in the foreign key.
-
setKeySequence
public void setKeySequence(int seq)
The sequence of this join in the foreign key.
-
getDeleteAction
public int getDeleteAction()
Return the delete action for the key. Will be one of:ACTION_NONE
,ACTION_RESTRICT
,ACTION_CASCADE
,ACTION_NULL
,ACTION_DEFAULT
.
-
setDeleteAction
public void setDeleteAction(int action)
Set the delete action for the key. Must be one of:ACTION_NONE
,ACTION_RESTRICT
,ACTION_CASCADE
,ACTION_NULL
,ACTION_DEFAULT
.
-
getUpdateAction
public int getUpdateAction()
Return the update action for the key. Will be one of:ACTION_NONE
,ACTION_RESTRICT
,ACTION_CASCADE
,ACTION_NULL
,ACTION_DEFAULT
.
-
setUpdateAction
public void setUpdateAction(int action)
Set the update action for the key. Must be one of:ACTION_NONE
,ACTION_RESTRICT
,ACTION_CASCADE
,ACTION_NULL
,ACTION_DEFAULT
.
-
getIndex
public int getIndex()
Return the foreign key's 0-based index in the owning table.
-
getPrimaryKeyColumn
public Column getPrimaryKeyColumn(Column local)
Return the primary key column joined to the given local column.
-
getColumn
public Column getColumn(Column pk)
Return the local column joined to the given primary key column.
-
getConstant
public java.lang.Object getConstant(Column local)
Return the constant value assigned to the given local column.
-
getPrimaryKeyConstant
public java.lang.Object getPrimaryKeyConstant(Column pk)
Return the constant value assigned to the given primary key column.
-
getColumns
public Column[] getColumns()
Return the local columns in the foreign key local table order.
-
getConstants
public java.lang.Object[] getConstants()
Return the constant values assigned to the local columns returned bygetConstantColumns()
.
-
getPrimaryKeyConstants
public java.lang.Object[] getPrimaryKeyConstants()
Return the constant values assigned to the primary key columns returned bygetConstantPrimaryKeyColumns()
.
-
containsColumn
public boolean containsColumn(Column col)
Return true if the fk includes the given local column.
-
containsPrimaryKeyColumn
public boolean containsPrimaryKeyColumn(Column col)
Return true if the fk includes the given primary key column.
-
containsConstantColumn
public boolean containsConstantColumn(Column col)
Return true if the fk includes the given local column.
-
containsConstantPrimaryKeyColumn
public boolean containsConstantPrimaryKeyColumn(Column col)
Return true if the fk includes the given primary key column.
-
getPrimaryKeyColumns
public Column[] getPrimaryKeyColumns()
Return the foreign columns in the foreign key, in join-order with the result ofgetColumns()
.
-
getConstantColumns
public Column[] getConstantColumns()
Return the local columns that we link to using constant values.
-
getConstantPrimaryKeyColumns
public Column[] getConstantPrimaryKeyColumns()
Return the primary key columns that we link to using constant values.
-
setConstantJoins
public void setConstantJoins(java.lang.Object[] consts, Column[] pkCols)
Set the foreign key's constant joins.
-
setConstantJoins
public void setConstantJoins(Column[] cols, java.lang.Object[] consts)
Set the foreign key's constant joins.
-
join
public void join(Column local, Column toPK)
Join a local column to a primary key column of another table.
-
joinConstant
public void joinConstant(java.lang.Object val, Column toPK)
Join a constant value to a primary key column of another table. The constant must be either a string or a number.
-
joinConstant
public void joinConstant(Column col, java.lang.Object val)
Join a constant value to a local column of this table. The constant must be either a string or a number.
-
removeJoin
public boolean removeJoin(Column col)
Remove any joins inolving the given column.- Returns:
- true if the join was removed, false if not part of the key
-
getPrimaryKeyTable
public Table getPrimaryKeyTable()
Returns the table this foreign key is linking to, if it is known yet.
-
refColumns
public void refColumns()
Ref all columns in this key.
-
derefColumns
public void derefColumns()
Deref all columns in this key.
-
equalsForeignKey
public boolean equalsForeignKey(ForeignKey fk)
Foreign keys are equal if the satisfy the equality constraints ofConstraint
and they have the same local and primary key columns and action.
-
columnsMatch
public boolean columnsMatch(Column[] fkCols, Column[] fkPKCols)
Return true if the given local and foreign columns match those on this key. This can be used to find foreign keys given only column linking information.
-
hasNotNullColumns
public boolean hasNotNullColumns()
Checks for non-nullable local columns.
-
loadNameFromDB
@Deprecated public java.lang.String loadNameFromDB(DBDictionary dbdict, java.sql.Connection conn)
Deprecated.Return the name of the foreignkey constraint as defined in the database.
-
loadIdentifierFromDB
public DBIdentifier loadIdentifierFromDB(DBDictionary dbdict, java.sql.Connection conn)
-
addColumn
public void addColumn(ForeignKey fk)
Joins the column of a single column FK to this FK.- Parameters:
fk
-
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classConstraint
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classConstraint
-
getRefCount
public int getRefCount()
-
ref
public void ref()
-
deref
public void deref()
-
-