HHH-8703 Remove need to support EntityKey de-serialization without having access to a SessionFactory

(cherry picked from commit 9a3b1417c6)

Conflicts:

	hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java
This commit is contained in:
Sanne Grinovero 2013-11-16 22:36:10 +00:00 committed by Steve Ebersole
parent cfefabcd39
commit f96eabc08e
6 changed files with 9 additions and 150 deletions

View File

@ -1,70 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.internal;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.type.Type;
public class EssentialEntityPersisterDetails implements EntityEssentials {
private final Type identifierType;
private final boolean isBatchLoadable;
private final String entityName;
private final String rootEntityName;
public EssentialEntityPersisterDetails(Type identifierType, boolean isBatchLoadable, String entityName, String rootEntityName) {
this.identifierType = identifierType;
this.isBatchLoadable = isBatchLoadable;
this.entityName = entityName;
this.rootEntityName = rootEntityName;
}
@Override
public Type getIdentifierType() {
return identifierType;
}
@Override
public boolean isBatchLoadable() {
return isBatchLoadable;
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public String getRootEntityName() {
return rootEntityName;
}
@Override
public SessionFactoryImplementor getFactory() {
return null;
}
}

View File

@ -1658,7 +1658,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
LOG.trace( "Serializing persistent-context" ); LOG.trace( "Serializing persistent-context" );
} }
final StatefulPersistenceContext rtn = new StatefulPersistenceContext( session ); final StatefulPersistenceContext rtn = new StatefulPersistenceContext( session );
SessionFactoryImplementor sfi = session==null ? null : session.getFactory(); SessionFactoryImplementor sfi = session.getFactory();
// during deserialization, we need to reconnect all proxies and // during deserialization, we need to reconnect all proxies and
// collections to this session, as well as the EntityEntry and // collections to this session, as well as the EntityEntry and

View File

@ -29,12 +29,9 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.engine.internal.EssentialEntityPersisterDetails;
import org.hibernate.internal.util.compare.EqualsHelper; import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.Type;
/** /**
* Uniquely identifies of an entity instance in a particular Session by identifier. * Uniquely identifies of an entity instance in a particular Session by identifier.
@ -53,7 +50,7 @@ public final class EntityKey implements Serializable {
private final Serializable identifier; private final Serializable identifier;
private final int hashCode; private final int hashCode;
private final EntityEssentials persister; private final EntityPersister persister;
/** /**
* Construct a unique identifier for an entity class instance. * Construct a unique identifier for an entity class instance.
@ -80,11 +77,11 @@ public final class EntityKey implements Serializable {
* and so both equals and hashcode implementations can't be implemented correctly. * and so both equals and hashcode implementations can't be implemented correctly.
* *
* @param identifier The identifier value * @param identifier The identifier value
* @param fakePersister Is a placeholder for the EntityPersister, only providing essential methods needed for this purpose. * @param persister The EntityPersister
* @param hashCode The hashCode needs to be provided as it can't be calculated correctly without the SessionFactory. * @param hashCode The hashCode needs to be provided as it can't be calculated correctly without the SessionFactory.
*/ */
private EntityKey(Serializable identifier, EntityEssentials fakePersister, int hashCode) { private EntityKey(Serializable identifier, EntityPersister persister, int hashCode) {
this.persister = fakePersister; this.persister = persister;
if ( identifier == null ) { if ( identifier == null ) {
throw new AssertionFailure( "null identifier" ); throw new AssertionFailure( "null identifier" );
} }
@ -158,12 +155,8 @@ public final class EntityKey implements Serializable {
* @throws IOException Thrown by Java I/O * @throws IOException Thrown by Java I/O
*/ */
public void serialize(ObjectOutputStream oos) throws IOException { public void serialize(ObjectOutputStream oos) throws IOException {
oos.writeObject( persister.getIdentifierType() );
oos.writeBoolean( isBatchLoadable() );
oos.writeObject( identifier ); oos.writeObject( identifier );
oos.writeObject( persister.getEntityName() ); oos.writeObject( persister.getEntityName() );
oos.writeObject( persister.getRootEntityName() );
oos.writeInt( hashCode );
} }
/** /**
@ -179,20 +172,9 @@ public final class EntityKey implements Serializable {
* @throws ClassNotFoundException Thrown by Java I/O * @throws ClassNotFoundException Thrown by Java I/O
*/ */
public static EntityKey deserialize(ObjectInputStream ois, SessionFactoryImplementor sessionFactory) throws IOException, ClassNotFoundException { public static EntityKey deserialize(ObjectInputStream ois, SessionFactoryImplementor sessionFactory) throws IOException, ClassNotFoundException {
final Type identifierType = (Type) ois.readObject();
final boolean isBatchLoadable = ois.readBoolean();
final Serializable id = (Serializable) ois.readObject(); final Serializable id = (Serializable) ois.readObject();
final String entityName = (String) ois.readObject(); final String entityName = (String) ois.readObject();
final String rootEntityName = (String) ois.readObject(); final EntityPersister entityPersister = sessionFactory.getEntityPersister( entityName );
final int hashCode = ois.readInt(); return new EntityKey(id, entityPersister);
if ( sessionFactory != null) {
final EntityPersister entityPersister = sessionFactory.getEntityPersister( entityName );
return new EntityKey(id, entityPersister);
}
else {
//This version will produce an EntityKey which is technically unable to satisfy the equals contract!
final EntityEssentials fakePersister = new EssentialEntityPersisterDetails(identifierType, isBatchLoadable, entityName, rootEntityName);
return new EntityKey(id, fakePersister, hashCode);
}
} }
} }

View File

@ -1,52 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.persister.entity;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;
/**
* This the internal contract exposed by EntityPersister to EntityKey instances.
* The purpose is to keep the number of fields for EntityKey to a minimum amount
* and still be able to set it the properties listed here without having to create
* a complete EntityPersister implementation.
*
* @see org.hibernate.persister.entity.EntityPersister
* @see org.hibernate.engine.spi.EntityKey
*
* @author Sanne Grinovero
*/
public interface EntityEssentials {
Type getIdentifierType();
boolean isBatchLoadable();
String getEntityName();
String getRootEntityName();
SessionFactoryImplementor getFactory();
}

View File

@ -60,7 +60,7 @@ import org.hibernate.type.VersionType;
* *
* @author Gavin King * @author Gavin King
*/ */
public interface EntityPersister extends OptimisticCacheSource, EntityEssentials { public interface EntityPersister extends OptimisticCacheSource {
/** /**
* The property name of the "special" identifier property in HQL * The property name of the "special" identifier property in HQL

View File

@ -29,7 +29,6 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.Type; import org.hibernate.type.Type;
@ -87,7 +86,7 @@ public final class MessageHelper {
* @return An info string, in the form [FooBar#1] * @return An info string, in the form [FooBar#1]
*/ */
public static String infoString( public static String infoString(
EntityEssentials persister, EntityPersister persister,
Object id, Object id,
SessionFactoryImplementor factory) { SessionFactoryImplementor factory) {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();