001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SecondaryKeyMetadata.java,v 1.13.2.2 2008/01/07 15:14:20 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.model;
010:
011: /**
012: * The metadata for a secondary key field. A secondary key may be specified
013: * with the {@link SecondaryKey} annotation.
014: *
015: * <p>{@code SecondaryKeyMetadata} objects are thread-safe. Multiple threads
016: * may safely call the methods of a shared {@code SecondaryKeyMetadata}
017: * object.</p>
018: *
019: * @author Mark Hayes
020: */
021: public class SecondaryKeyMetadata extends FieldMetadata {
022:
023: private static final long serialVersionUID = 8118924993396722502L;
024:
025: private String keyName;
026: private Relationship relationship;
027: private String elementClassName;
028: private String relatedEntity;
029: private DeleteAction deleteAction;
030:
031: /**
032: * Used by an {@code EntityModel} to construct secondary key metadata.
033: */
034: public SecondaryKeyMetadata(String name, String className,
035: String declaringClassName, String elementClassName,
036: String keyName, Relationship relationship,
037: String relatedEntity, DeleteAction deleteAction) {
038: super (name, className, declaringClassName);
039: this .elementClassName = elementClassName;
040: this .keyName = keyName;
041: this .relationship = relationship;
042: this .relatedEntity = relatedEntity;
043: this .deleteAction = deleteAction;
044: }
045:
046: /**
047: * Returns the class name of the array or collection element for a {@link
048: * Relationship#ONE_TO_MANY ONE_TO_MANY} or {@link
049: * Relationship#MANY_TO_MANY MANY_TO_MANY} relationship, or null for a
050: * Relationship#ONE_TO_ONE ONE_TO_ONE} or {@link Relationship#MANY_TO_ONE
051: * MANY_TO_ONE} relationship.
052: */
053: public String getElementClassName() {
054: return elementClassName;
055: }
056:
057: /**
058: * Returns the key name, which may be different from the field name.
059: */
060: public String getKeyName() {
061: return keyName;
062: }
063:
064: /**
065: * Returns the relationship between instances of the entity class and the
066: * secondary keys. This may be specified using the {@link
067: * SecondaryKey#relate} annotation.
068: */
069: public Relationship getRelationship() {
070: return relationship;
071: }
072:
073: /**
074: * Returns the class name of the related (foreign) entity, for which
075: * foreign key constraints are specified using the {@link
076: * SecondaryKey#relatedEntity} annotation.
077: */
078: public String getRelatedEntity() {
079: return relatedEntity;
080: }
081:
082: /**
083: * Returns the action to take when a related entity is deleted having a
084: * primary key value that exists as a secondary key value for this entity.
085: * This may be specified using the {@link
086: * SecondaryKey#onRelatedEntityDelete} annotation.
087: */
088: public DeleteAction getDeleteAction() {
089: return deleteAction;
090: }
091:
092: @Override
093: public boolean equals(Object other) {
094: if (other instanceof SecondaryKeyMetadata) {
095: SecondaryKeyMetadata o = (SecondaryKeyMetadata) other;
096: return super .equals(o)
097: && relationship == o.relationship
098: && deleteAction == o.deleteAction
099: && ClassMetadata.nullOrEqual(keyName, o.keyName)
100: && ClassMetadata.nullOrEqual(elementClassName,
101: o.elementClassName)
102: && ClassMetadata.nullOrEqual(relatedEntity,
103: o.relatedEntity);
104: } else {
105: return false;
106: }
107: }
108:
109: @Override
110: public int hashCode() {
111: return super.hashCode() + relationship.hashCode()
112: + deleteAction.hashCode()
113: + ClassMetadata.hashCode(keyName)
114: + ClassMetadata.hashCode(elementClassName)
115: + ClassMetadata.hashCode(relatedEntity);
116: }
117: }
|