~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/engine/EntityKey.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: EntityKey.java 9194 2006-02-01 19:59:07Z steveebersole $
 
2
package org.hibernate.engine;
 
3
 
 
4
import java.io.Serializable;
 
5
import java.io.ObjectOutputStream;
 
6
import java.io.IOException;
 
7
import java.io.ObjectInputStream;
 
8
 
 
9
import org.hibernate.AssertionFailure;
 
10
import org.hibernate.EntityMode;
 
11
import org.hibernate.util.SerializationHelper;
 
12
import org.hibernate.persister.entity.EntityPersister;
 
13
import org.hibernate.pretty.MessageHelper;
 
14
import org.hibernate.type.Type;
 
15
 
 
16
/**
 
17
 * Uniquely identifies of an entity instance in a particular session by identifier.
 
18
 * <p/>
 
19
 * Uniqueing information consists of the entity-name and the identifier value.
 
20
 *
 
21
 * @see EntityUniqueKey
 
22
 * @author Gavin King
 
23
 */
 
24
public final class EntityKey implements Serializable {
 
25
        private final Serializable identifier;
 
26
        private final String rootEntityName;
 
27
        private final String entityName;
 
28
        private final Type identifierType;
 
29
        private final boolean isBatchLoadable;
 
30
        private final SessionFactoryImplementor factory;
 
31
        private final int hashCode;
 
32
        private final EntityMode entityMode;
 
33
 
 
34
        /**
 
35
         * Construct a unique identifier for an entity class instance
 
36
         */
 
37
        public EntityKey(Serializable id, EntityPersister persister, EntityMode entityMode) {
 
38
                if ( id == null ) {
 
39
                        throw new AssertionFailure( "null identifier" );
 
40
                }
 
41
                this.identifier = id; 
 
42
                this.entityMode = entityMode;
 
43
                this.rootEntityName = persister.getRootEntityName();
 
44
                this.entityName = persister.getEntityName();
 
45
                this.identifierType = persister.getIdentifierType();
 
46
                this.isBatchLoadable = persister.isBatchLoadable();
 
47
                this.factory = persister.getFactory();
 
48
                hashCode = generateHashCode(); //cache the hashcode
 
49
        }
 
50
 
 
51
        /**
 
52
         * Used to reconstruct an EntityKey during deserialization.
 
53
         *
 
54
         * @param identifier The identifier value
 
55
         * @param rootEntityName The root entity name
 
56
         * @param entityName The specific entity name
 
57
         * @param identifierType The type of the identifier value
 
58
         * @param batchLoadable Whether represented entity is eligible for batch loading
 
59
         * @param factory The session factory
 
60
         * @param entityMode The entity's entity mode
 
61
         */
 
62
        private EntityKey(
 
63
                        Serializable identifier,
 
64
                String rootEntityName,
 
65
                String entityName,
 
66
                Type identifierType,
 
67
                boolean batchLoadable,
 
68
                SessionFactoryImplementor factory,
 
69
                EntityMode entityMode) {
 
70
                this.identifier = identifier;
 
71
                this.rootEntityName = rootEntityName;
 
72
                this.entityName = entityName;
 
73
                this.identifierType = identifierType;
 
74
                this.isBatchLoadable = batchLoadable;
 
75
                this.factory = factory;
 
76
                this.entityMode = entityMode;
 
77
                this.hashCode = generateHashCode();
 
78
        }
 
79
 
 
80
        public boolean isBatchLoadable() {
 
81
                return isBatchLoadable;
 
82
        }
 
83
 
 
84
        /**
 
85
         * Get the user-visible identifier
 
86
         */
 
87
        public Serializable getIdentifier() {
 
88
                return identifier;
 
89
        }
 
90
 
 
91
        public String getEntityName() {
 
92
                return entityName;
 
93
        }
 
94
 
 
95
        public boolean equals(Object other) {
 
96
                EntityKey otherKey = (EntityKey) other;
 
97
                return otherKey.rootEntityName.equals(this.rootEntityName) && 
 
98
                        identifierType.isEqual(otherKey.identifier, this.identifier, entityMode, factory);
 
99
        }
 
100
        
 
101
        private int generateHashCode() {
 
102
                int result = 17;
 
103
                result = 37 * result + rootEntityName.hashCode();
 
104
                result = 37 * result + identifierType.getHashCode( identifier, entityMode, factory );
 
105
                return result;
 
106
        }
 
107
 
 
108
        public int hashCode() {
 
109
                return hashCode;
 
110
        }
 
111
 
 
112
        public String toString() {
 
113
                return "EntityKey" + 
 
114
                        MessageHelper.infoString( factory.getEntityPersister( entityName ), identifier, factory );
 
115
        }
 
116
 
 
117
        /**
 
118
         * Custom serialization routine used during serialization of a
 
119
         * Session/PersistenceContext for increased performance.
 
120
         *
 
121
         * @param oos The stream to which we should write the serial data.
 
122
         * @throws IOException
 
123
         */
 
124
        void serialize(ObjectOutputStream oos) throws IOException {
 
125
                oos.writeObject( identifier );
 
126
                oos.writeObject( rootEntityName );
 
127
                oos.writeObject( entityName );
 
128
                oos.writeObject( identifierType );
 
129
                oos.writeBoolean( isBatchLoadable );
 
130
                oos.writeObject( entityMode.toString() );
 
131
        }
 
132
 
 
133
        /**
 
134
         * Custom deserialization routine used during deserialization of a
 
135
         * Session/PersistenceContext for increased performance.
 
136
         *
 
137
         * @param ois The stream from which to read the entry.
 
138
         * @param session The session being deserialized.
 
139
         * @return The deserialized EntityEntry
 
140
         * @throws IOException
 
141
         * @throws ClassNotFoundException
 
142
         */
 
143
        static EntityKey deserialize(
 
144
                        ObjectInputStream ois,
 
145
                SessionImplementor session) throws IOException, ClassNotFoundException {
 
146
                return new EntityKey(
 
147
                                ( Serializable ) ois.readObject(),
 
148
                        ( String ) ois.readObject(),
 
149
                        ( String ) ois.readObject(),
 
150
                        ( Type ) ois.readObject(),
 
151
                        ois.readBoolean(),
 
152
                        session.getFactory(),
 
153
                        EntityMode.parse( ( String ) ois.readObject() )
 
154
                );
 
155
        }
 
156
}