From f96eabc08e7802e46786c7b11e673354db2dbfca Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Sat, 16 Nov 2013 22:36:10 +0000 Subject: [PATCH] HHH-8703 Remove need to support EntityKey de-serialization without having access to a SessionFactory (cherry picked from commit 9a3b1417c63662c5fbd212ae615c7d5168b3b7c4) Conflicts: hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java --- .../EssentialEntityPersisterDetails.java | 70 ------------------- .../internal/StatefulPersistenceContext.java | 2 +- .../org/hibernate/engine/spi/EntityKey.java | 30 ++------ .../persister/entity/EntityEssentials.java | 52 -------------- .../persister/entity/EntityPersister.java | 2 +- .../org/hibernate/pretty/MessageHelper.java | 3 +- 6 files changed, 9 insertions(+), 150 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/engine/internal/EssentialEntityPersisterDetails.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/persister/entity/EntityEssentials.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/EssentialEntityPersisterDetails.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/EssentialEntityPersisterDetails.java deleted file mode 100644 index 3c81ef4245..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/EssentialEntityPersisterDetails.java +++ /dev/null @@ -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; - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index 40c37e5ea6..02d4b8c6a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -1658,7 +1658,7 @@ public class StatefulPersistenceContext implements PersistenceContext { LOG.trace( "Serializing persistent-context" ); } 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 // collections to this session, as well as the EntityEntry and diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java index 35cf8e39b0..bd1fad0aeb 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java @@ -29,12 +29,9 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import org.hibernate.AssertionFailure; -import org.hibernate.engine.internal.EssentialEntityPersisterDetails; import org.hibernate.internal.util.compare.EqualsHelper; -import org.hibernate.persister.entity.EntityEssentials; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.pretty.MessageHelper; -import org.hibernate.type.Type; /** * 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 int hashCode; - private final EntityEssentials persister; + private final EntityPersister persister; /** * 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. * * @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. */ - private EntityKey(Serializable identifier, EntityEssentials fakePersister, int hashCode) { - this.persister = fakePersister; + private EntityKey(Serializable identifier, EntityPersister persister, int hashCode) { + this.persister = persister; if ( identifier == null ) { throw new AssertionFailure( "null identifier" ); } @@ -158,12 +155,8 @@ public final class EntityKey implements Serializable { * @throws IOException Thrown by Java I/O */ public void serialize(ObjectOutputStream oos) throws IOException { - oos.writeObject( persister.getIdentifierType() ); - oos.writeBoolean( isBatchLoadable() ); oos.writeObject( identifier ); 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 */ 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 String entityName = (String) ois.readObject(); - final String rootEntityName = (String) ois.readObject(); - final int hashCode = ois.readInt(); - 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); - } + final EntityPersister entityPersister = sessionFactory.getEntityPersister( entityName ); + return new EntityKey(id, entityPersister); } } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityEssentials.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityEssentials.java deleted file mode 100644 index 1a379e0453..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityEssentials.java +++ /dev/null @@ -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(); - -} diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java index 8396242adc..6291a71ce5 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java @@ -60,7 +60,7 @@ import org.hibernate.type.VersionType; * * @author Gavin King */ -public interface EntityPersister extends OptimisticCacheSource, EntityEssentials { +public interface EntityPersister extends OptimisticCacheSource { /** * The property name of the "special" identifier property in HQL diff --git a/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java b/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java index 85e1400247..f181f72ff0 100644 --- a/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java @@ -29,7 +29,6 @@ import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.persister.collection.CollectionPersister; -import org.hibernate.persister.entity.EntityEssentials; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; @@ -87,7 +86,7 @@ public final class MessageHelper { * @return An info string, in the form [FooBar#1] */ public static String infoString( - EntityEssentials persister, + EntityPersister persister, Object id, SessionFactoryImplementor factory) { StringBuilder s = new StringBuilder();