From 0a2a5c622e3eb30724e80bc8661c0ac55ebfb2be Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 13 Sep 2016 21:10:23 -0500 Subject: [PATCH] HHH-11097 - Performance problem if cached entity has attribute state with an expensive toString() method (LOB, etc) --- .../org/hibernate/type/CollectionType.java | 24 +++++++++++++++++-- .../java/org/hibernate/type/EntityType.java | 11 +++++++-- .../descriptor/java/BlobTypeDescriptor.java | 2 +- .../descriptor/java/ClobTypeDescriptor.java | 2 +- .../descriptor/java/NClobTypeDescriptor.java | 2 +- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java b/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java index dbdab397b7..bbfdda0f99 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java @@ -179,7 +179,23 @@ public abstract class CollectionType extends AbstractType implements Association if ( value == null ) { return "null"; } - else if ( !Hibernate.isInitialized( value ) ) { + + if ( !getReturnedClass().isInstance( value ) && !PersistentCollection.class.isInstance( value ) ) { + // its most likely the collection-key + final CollectionPersister persister = getPersister( factory ); + if ( persister.getKeyType().getReturnedClass().isInstance( value ) ) { + return getRole() + "#" + getPersister( factory ).getKeyType().toLoggableString( value, factory ); + } + else { + // although it could also be the collection-id + if ( persister.getIdentifierType() != null + && persister.getIdentifierType().getReturnedClass().isInstance( value ) ) { + return getRole() + "#" + getPersister( factory ).getIdentifierType().toLoggableString( value, factory ); + } + } + } + + if ( !Hibernate.isInitialized( value ) ) { return ""; } else { @@ -289,7 +305,11 @@ public abstract class CollectionType extends AbstractType implements Association * @return The underlying collection persister */ private CollectionPersister getPersister(SharedSessionContractImplementor session) { - return session.getFactory().getMetamodel().collectionPersister( role ); + return getPersister( session.getFactory() ); + } + + private CollectionPersister getPersister(SessionFactoryImplementor factory) { + return factory.getMetamodel().collectionPersister( role ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java index 84c0e012a4..262bd6e09a 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java @@ -499,8 +499,15 @@ public abstract class EntityType extends AbstractType implements AssociationType return "null"; } - EntityPersister persister = getAssociatedEntityPersister( factory ); - StringBuilder result = new StringBuilder().append( associatedEntityName ); + final EntityPersister persister = getAssociatedEntityPersister( factory ); + if ( !persister.getEntityTuplizer().isInstance( value ) ) { + // it should be the id type... + if ( persister.getIdentifierType().getReturnedClass().isInstance( value ) ) { + return associatedEntityName + "#" + value; + } + } + + final StringBuilder result = new StringBuilder().append( associatedEntityName ); if ( persister.hasIdentifierProperty() ) { final Serializable id; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobTypeDescriptor.java index 328a247ffa..5de59fbff6 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobTypeDescriptor.java @@ -63,7 +63,7 @@ public class BlobTypeDescriptor extends AbstractTypeDescriptor { @Override public String extractLoggableRepresentation(Blob value) { - return value == null ? "null" : "BLOB{...}"; + return value == null ? "null" : "{blob}"; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobTypeDescriptor.java index e2f6a61fab..5d97cdd9a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobTypeDescriptor.java @@ -57,7 +57,7 @@ public class ClobTypeDescriptor extends AbstractTypeDescriptor { @Override public String extractLoggableRepresentation(Clob value) { - return value == null ? "null" : "CLOB{...}"; + return value == null ? "null" : "{clob}"; } public String toString(Clob value) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/NClobTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/NClobTypeDescriptor.java index 50107b695e..a706a0dcf1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/NClobTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/NClobTypeDescriptor.java @@ -57,7 +57,7 @@ public class NClobTypeDescriptor extends AbstractTypeDescriptor { @Override public String extractLoggableRepresentation(NClob value) { - return value == null ? "null" : "NCLOB{...}"; + return value == null ? "null" : "{nclob}"; } public String toString(NClob value) {