KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > EntityKey


1 //$Id: EntityKey.java,v 1.10 2005/02/21 11:50:29 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.AssertionFailure;
7 import org.hibernate.EntityMode;
8 import org.hibernate.persister.entity.EntityPersister;
9 import org.hibernate.pretty.MessageHelper;
10 import org.hibernate.type.Type;
11
12 /**
13  * Uniquely identifies of an entity instance in a particular session.
14  *
15  * Consisting of the user-visible identifier and the identifier space (eg. tablename).
16  *
17  * @author Gavin King
18  */

19 public final class EntityKey implements Serializable JavaDoc {
20     private final Serializable JavaDoc identifier;
21     private final Serializable JavaDoc rootEntityName;
22     private final String JavaDoc entityName;
23     private final Type identifierType;
24     private final boolean isBatchLoadable;
25     private final SessionFactoryImplementor factory;
26     private final int hashCode;
27     private final EntityMode entityMode;
28
29     /**
30      * Construct a unique identifier for an entity class instance
31      */

32     public EntityKey(Serializable JavaDoc id, EntityPersister persister, EntityMode entityMode) {
33         if (id==null) throw new AssertionFailure("null identifier");
34         this.identifier = id;
35         this.entityMode = entityMode;
36         this.rootEntityName = persister.getRootEntityName();
37         this.entityName = persister.getEntityName();
38         this.identifierType = persister.getIdentifierType();
39         this.isBatchLoadable = persister.isBatchLoadable();
40         this.factory = persister.getFactory();
41         hashCode = getHashCode(); //cache the hashcode
42
}
43     
44     public boolean isBatchLoadable() {
45         return isBatchLoadable;
46     }
47
48     /**
49      * Get the user-visible identifier
50      */

51     public Serializable JavaDoc getIdentifier() {
52         return identifier;
53     }
54
55     public String JavaDoc getEntityName() {
56         return entityName;
57     }
58
59     public boolean equals(Object JavaDoc other) {
60         EntityKey otherKey = (EntityKey) other;
61         return otherKey.rootEntityName.equals(this.rootEntityName) &&
62             identifierType.isEqual(otherKey.identifier, this.identifier, entityMode, factory);
63     }
64     
65     private int getHashCode() {
66         int result = 17;
67         result = 37 * result + rootEntityName.hashCode();
68         result = 37 * result + identifierType.getHashCode(identifier, entityMode, factory);
69         return result;
70     }
71
72     public int hashCode() {
73         return hashCode;
74     }
75
76     public String JavaDoc toString() {
77         return "EntityKey" +
78             MessageHelper.infoString( factory.getEntityPersister(entityName), identifier, factory );
79     }
80
81 }
82
Popular Tags