1
//$Id: EntityKey.java 9194 2006-02-01 19:59:07Z steveebersole $
2
package org.hibernate.engine;
4
import java.io.Serializable;
5
import java.io.ObjectOutputStream;
6
import java.io.IOException;
7
import java.io.ObjectInputStream;
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;
17
* Uniquely identifies of an entity instance in a particular session by identifier.
19
* Uniqueing information consists of the entity-name and the identifier value.
21
* @see EntityUniqueKey
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;
35
* Construct a unique identifier for an entity class instance
37
public EntityKey(Serializable id, EntityPersister persister, EntityMode entityMode) {
39
throw new AssertionFailure( "null identifier" );
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
52
* Used to reconstruct an EntityKey during deserialization.
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
63
Serializable identifier,
64
String rootEntityName,
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();
80
public boolean isBatchLoadable() {
81
return isBatchLoadable;
85
* Get the user-visible identifier
87
public Serializable getIdentifier() {
91
public String getEntityName() {
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);
101
private int generateHashCode() {
103
result = 37 * result + rootEntityName.hashCode();
104
result = 37 * result + identifierType.getHashCode( identifier, entityMode, factory );
108
public int hashCode() {
112
public String toString() {
114
MessageHelper.infoString( factory.getEntityPersister( entityName ), identifier, factory );
118
* Custom serialization routine used during serialization of a
119
* Session/PersistenceContext for increased performance.
121
* @param oos The stream to which we should write the serial data.
122
* @throws IOException
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() );
134
* Custom deserialization routine used during deserialization of a
135
* Session/PersistenceContext for increased performance.
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
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(),
152
session.getFactory(),
153
EntityMode.parse( ( String ) ois.readObject() )