1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.persist.model; 10 11/** 12 * The metadata for a secondary key field. A secondary key may be specified 13 * with the {@link SecondaryKey} annotation. 14 * 15 * <p>{@code SecondaryKeyMetadata} objects are thread-safe. Multiple threads 16 * may safely call the methods of a shared {@code SecondaryKeyMetadata} 17 * object.</p> 18 * 19 * @author Mark Hayes 20 */ 21public class SecondaryKeyMetadata extends FieldMetadata { 22 23 private static final long serialVersionUID = 8118924993396722502L; 24 25 private String keyName; 26 private Relationship relationship; 27 private String elementClassName; 28 private String relatedEntity; 29 private DeleteAction deleteAction; 30 31 /** 32 * Used by an {@code EntityModel} to construct secondary key metadata. 33 */ 34 public SecondaryKeyMetadata(String name, 35 String className, 36 String declaringClassName, 37 String elementClassName, 38 String keyName, 39 Relationship relationship, 40 String relatedEntity, 41 DeleteAction deleteAction) { 42 super(name, className, declaringClassName); 43 this.elementClassName = elementClassName; 44 this.keyName = keyName; 45 this.relationship = relationship; 46 this.relatedEntity = relatedEntity; 47 this.deleteAction = deleteAction; 48 } 49 50 /** 51 * Returns the class name of the array or collection element for a {@link 52 * Relationship#ONE_TO_MANY ONE_TO_MANY} or {@link 53 * Relationship#MANY_TO_MANY MANY_TO_MANY} relationship, or null for a 54 * Relationship#ONE_TO_ONE ONE_TO_ONE} or {@link Relationship#MANY_TO_ONE 55 * MANY_TO_ONE} relationship. 56 */ 57 public String getElementClassName() { 58 return elementClassName; 59 } 60 61 /** 62 * Returns the key name, which may be different from the field name. 63 */ 64 public String getKeyName() { 65 return keyName; 66 } 67 68 /** 69 * Returns the relationship between instances of the entity class and the 70 * secondary keys. This may be specified using the {@link 71 * SecondaryKey#relate} annotation. 72 */ 73 public Relationship getRelationship() { 74 return relationship; 75 } 76 77 /** 78 * Returns the class name of the related (foreign) entity, for which 79 * foreign key constraints are specified using the {@link 80 * SecondaryKey#relatedEntity} annotation. 81 */ 82 public String getRelatedEntity() { 83 return relatedEntity; 84 } 85 86 /** 87 * Returns the action to take when a related entity is deleted having a 88 * primary key value that exists as a secondary key value for this entity. 89 * This may be specified using the {@link 90 * SecondaryKey#onRelatedEntityDelete} annotation. 91 */ 92 public DeleteAction getDeleteAction() { 93 return deleteAction; 94 } 95 96 @Override 97 public boolean equals(Object other) { 98 if (other instanceof SecondaryKeyMetadata) { 99 SecondaryKeyMetadata o = (SecondaryKeyMetadata) other; 100 return super.equals(o) && 101 relationship == o.relationship && 102 ClassMetadata.nullOrEqual(deleteAction, o.deleteAction) && 103 ClassMetadata.nullOrEqual(keyName, o.keyName) && 104 ClassMetadata.nullOrEqual(elementClassName, 105 o.elementClassName) && 106 ClassMetadata.nullOrEqual(relatedEntity, o.relatedEntity); 107 } else { 108 return false; 109 } 110 } 111 112 @Override 113 public int hashCode() { 114 return super.hashCode() + 115 relationship.hashCode() + 116 ClassMetadata.hashCode(deleteAction) + 117 ClassMetadata.hashCode(keyName) + 118 ClassMetadata.hashCode(elementClassName) + 119 ClassMetadata.hashCode(relatedEntity); 120 } 121} 122