001: //$Id: EntityUniqueKey.java 9194 2006-02-01 19:59:07Z steveebersole $
002: package org.hibernate.engine;
003:
004: import org.hibernate.EntityMode;
005: import org.hibernate.pretty.MessageHelper;
006: import org.hibernate.type.Type;
007:
008: import java.io.Serializable;
009: import java.io.ObjectOutputStream;
010: import java.io.IOException;
011: import java.io.ObjectInputStream;
012:
013: /**
014: * Used to uniquely key an entity instance in relation to a particular session
015: * by some unique property reference, as opposed to identifier.
016: * <p/>
017: * Uniqueing information consists of the entity-name, the referenced
018: * property name, and the referenced property value.
019: *
020: * @see EntityKey
021: * @author Gavin King
022: */
023: public class EntityUniqueKey implements Serializable {
024: private final String uniqueKeyName;
025: private final String entityName;
026: private final Object key;
027: private final Type keyType;
028: private final EntityMode entityMode;
029: private final int hashCode;
030:
031: public EntityUniqueKey(final String entityName,
032: final String uniqueKeyName, final Object semiResolvedKey,
033: final Type keyType, final EntityMode entityMode,
034: final SessionFactoryImplementor factory) {
035: this .uniqueKeyName = uniqueKeyName;
036: this .entityName = entityName;
037: this .key = semiResolvedKey;
038: this .keyType = keyType.getSemiResolvedType(factory);
039: this .entityMode = entityMode;
040: this .hashCode = generateHashCode(factory);
041: }
042:
043: public String getEntityName() {
044: return entityName;
045: }
046:
047: public Object getKey() {
048: return key;
049: }
050:
051: public String getUniqueKeyName() {
052: return uniqueKeyName;
053: }
054:
055: public int generateHashCode(SessionFactoryImplementor factory) {
056: int result = 17;
057: result = 37 * result + entityName.hashCode();
058: result = 37 * result + uniqueKeyName.hashCode();
059: result = 37 * result
060: + keyType.getHashCode(key, entityMode, factory);
061: return result;
062: }
063:
064: public int hashCode() {
065: return hashCode;
066: }
067:
068: public boolean equals(Object other) {
069: EntityUniqueKey that = (EntityUniqueKey) other;
070: return that.entityName.equals(entityName)
071: && that.uniqueKeyName.equals(uniqueKeyName)
072: && keyType.isEqual(that.key, key, entityMode);
073: }
074:
075: public String toString() {
076: return "EntityUniqueKey"
077: + MessageHelper.infoString(entityName, uniqueKeyName,
078: key);
079: }
080:
081: private void writeObject(ObjectOutputStream oos) throws IOException {
082: checkAbilityToSerialize();
083: oos.defaultWriteObject();
084: }
085:
086: private void checkAbilityToSerialize() {
087: // The unique property value represented here may or may not be
088: // serializable, so we do an explicit check here in order to generate
089: // a better error message
090: if (key != null
091: && !Serializable.class.isAssignableFrom(key.getClass())) {
092: throw new IllegalStateException(
093: "Cannot serialize an EntityUniqueKey which represents a non "
094: + "serializable property value ["
095: + entityName + "." + uniqueKeyName + "]");
096: }
097: }
098:
099: /**
100: * Custom serialization routine used during serialization of a
101: * Session/PersistenceContext for increased performance.
102: *
103: * @param oos The stream to which we should write the serial data.
104: * @throws IOException
105: */
106: void serialize(ObjectOutputStream oos) throws IOException {
107: checkAbilityToSerialize();
108: oos.writeObject(uniqueKeyName);
109: oos.writeObject(entityName);
110: oos.writeObject(key);
111: oos.writeObject(keyType);
112: oos.writeObject(entityMode);
113: }
114:
115: /**
116: * Custom deserialization routine used during deserialization of a
117: * Session/PersistenceContext for increased performance.
118: *
119: * @param ois The stream from which to read the entry.
120: * @param session The session being deserialized.
121: * @return The deserialized EntityEntry
122: * @throws IOException
123: * @throws ClassNotFoundException
124: */
125: static EntityUniqueKey deserialize(ObjectInputStream ois,
126: SessionImplementor session) throws IOException,
127: ClassNotFoundException {
128: return new EntityUniqueKey((String) ois.readObject(),
129: (String) ois.readObject(), ois.readObject(), (Type) ois
130: .readObject(), (EntityMode) ois.readObject(),
131: session.getFactory());
132: }
133: }
|