001: //$Id: EntityKey.java 9194 2006-02-01 19:59:07Z steveebersole $
002: package org.hibernate.engine;
003:
004: import java.io.Serializable;
005: import java.io.ObjectOutputStream;
006: import java.io.IOException;
007: import java.io.ObjectInputStream;
008:
009: import org.hibernate.AssertionFailure;
010: import org.hibernate.EntityMode;
011: import org.hibernate.util.SerializationHelper;
012: import org.hibernate.persister.entity.EntityPersister;
013: import org.hibernate.pretty.MessageHelper;
014: import org.hibernate.type.Type;
015:
016: /**
017: * Uniquely identifies of an entity instance in a particular session by identifier.
018: * <p/>
019: * Uniqueing information consists of the entity-name and the identifier value.
020: *
021: * @see EntityUniqueKey
022: * @author Gavin King
023: */
024: public final class EntityKey implements Serializable {
025: private final Serializable identifier;
026: private final String rootEntityName;
027: private final String entityName;
028: private final Type identifierType;
029: private final boolean isBatchLoadable;
030: private final SessionFactoryImplementor factory;
031: private final int hashCode;
032: private final EntityMode entityMode;
033:
034: /**
035: * Construct a unique identifier for an entity class instance
036: */
037: public EntityKey(Serializable id, EntityPersister persister,
038: EntityMode entityMode) {
039: if (id == null) {
040: throw new AssertionFailure("null identifier");
041: }
042: this .identifier = id;
043: this .entityMode = entityMode;
044: this .rootEntityName = persister.getRootEntityName();
045: this .entityName = persister.getEntityName();
046: this .identifierType = persister.getIdentifierType();
047: this .isBatchLoadable = persister.isBatchLoadable();
048: this .factory = persister.getFactory();
049: hashCode = generateHashCode(); //cache the hashcode
050: }
051:
052: /**
053: * Used to reconstruct an EntityKey during deserialization.
054: *
055: * @param identifier The identifier value
056: * @param rootEntityName The root entity name
057: * @param entityName The specific entity name
058: * @param identifierType The type of the identifier value
059: * @param batchLoadable Whether represented entity is eligible for batch loading
060: * @param factory The session factory
061: * @param entityMode The entity's entity mode
062: */
063: private EntityKey(Serializable identifier, String rootEntityName,
064: String entityName, Type identifierType,
065: boolean batchLoadable, SessionFactoryImplementor factory,
066: EntityMode entityMode) {
067: this .identifier = identifier;
068: this .rootEntityName = rootEntityName;
069: this .entityName = entityName;
070: this .identifierType = identifierType;
071: this .isBatchLoadable = batchLoadable;
072: this .factory = factory;
073: this .entityMode = entityMode;
074: this .hashCode = generateHashCode();
075: }
076:
077: public boolean isBatchLoadable() {
078: return isBatchLoadable;
079: }
080:
081: /**
082: * Get the user-visible identifier
083: */
084: public Serializable getIdentifier() {
085: return identifier;
086: }
087:
088: public String getEntityName() {
089: return entityName;
090: }
091:
092: public boolean equals(Object other) {
093: EntityKey otherKey = (EntityKey) other;
094: return otherKey.rootEntityName.equals(this .rootEntityName)
095: && identifierType.isEqual(otherKey.identifier,
096: this .identifier, entityMode, factory);
097: }
098:
099: private int generateHashCode() {
100: int result = 17;
101: result = 37 * result + rootEntityName.hashCode();
102: result = 37
103: * result
104: + identifierType.getHashCode(identifier, entityMode,
105: factory);
106: return result;
107: }
108:
109: public int hashCode() {
110: return hashCode;
111: }
112:
113: public String toString() {
114: return "EntityKey"
115: + MessageHelper.infoString(factory
116: .getEntityPersister(entityName), identifier,
117: factory);
118: }
119:
120: /**
121: * Custom serialization routine used during serialization of a
122: * Session/PersistenceContext for increased performance.
123: *
124: * @param oos The stream to which we should write the serial data.
125: * @throws IOException
126: */
127: void serialize(ObjectOutputStream oos) throws IOException {
128: oos.writeObject(identifier);
129: oos.writeObject(rootEntityName);
130: oos.writeObject(entityName);
131: oos.writeObject(identifierType);
132: oos.writeBoolean(isBatchLoadable);
133: oos.writeObject(entityMode.toString());
134: }
135:
136: /**
137: * Custom deserialization routine used during deserialization of a
138: * Session/PersistenceContext for increased performance.
139: *
140: * @param ois The stream from which to read the entry.
141: * @param session The session being deserialized.
142: * @return The deserialized EntityEntry
143: * @throws IOException
144: * @throws ClassNotFoundException
145: */
146: static EntityKey deserialize(ObjectInputStream ois,
147: SessionImplementor session) throws IOException,
148: ClassNotFoundException {
149: return new EntityKey((Serializable) ois.readObject(),
150: (String) ois.readObject(), (String) ois.readObject(),
151: (Type) ois.readObject(), ois.readBoolean(), session
152: .getFactory(), EntityMode.parse((String) ois
153: .readObject()));
154: }
155: }
|