From 820fe56aa46d5367122c7cca35a13608da63cee2 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 29 Oct 2020 09:36:53 +0000 Subject: [PATCH] HHH-14305 Memory savings in SingleTableEntityPersister --- .../internal/AbstractIdentifiableType.java | 25 +++++++++++++------ .../entity/SingleTableEntityPersister.java | 3 +++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java index ed7b041701..54a93363c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java @@ -7,6 +7,7 @@ package org.hibernate.metamodel.model.domain.internal; import java.io.Serializable; +import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.function.Consumer; @@ -207,6 +208,11 @@ public abstract class AbstractIdentifiableType @SuppressWarnings("unchecked") public void collectIdClassAttributes(Set> attributes) { if ( idClassAttributes != null ) { + //Make it writeable, if it was using the EMPTY_SET marker. + // This is because there's a semantic difference in this class between a null and an empty Set. + if ( idClassAttributes == Collections.EMPTY_SET ) { + idClassAttributes = new HashSet<>(); + } attributes.addAll( idClassAttributes ); } else if ( getSuperType() != null ) { @@ -305,14 +311,19 @@ public abstract class AbstractIdentifiableType @Override public void applyIdClassAttributes(Set> idClassAttributes) { - for ( SingularAttribute idClassAttribute : idClassAttributes ) { - if ( AbstractIdentifiableType.this == idClassAttribute.getDeclaringType() ) { - @SuppressWarnings({ "unchecked" }) - SingularPersistentAttribute declaredAttribute = (SingularPersistentAttribute) idClassAttribute; - addAttribute( declaredAttribute ); - } + if ( idClassAttributes.isEmpty() ) { + AbstractIdentifiableType.this.idClassAttributes = Collections.EMPTY_SET; + } + else { + for ( SingularAttribute idClassAttribute : idClassAttributes ) { + if ( AbstractIdentifiableType.this == idClassAttribute.getDeclaringType() ) { + @SuppressWarnings({ "unchecked" }) + SingularPersistentAttribute declaredAttribute = (SingularPersistentAttribute) idClassAttribute; + addAttribute( declaredAttribute ); + } + } + AbstractIdentifiableType.this.idClassAttributes = idClassAttributes; } - AbstractIdentifiableType.this.idClassAttributes = idClassAttributes; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java index b7ad8a73c6..9086a449d2 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java @@ -8,6 +8,7 @@ package org.hibernate.persister.entity; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -277,6 +278,8 @@ public class SingleTableEntityPersister extends AbstractEntityPersister { isNullableSubclassTable = ArrayHelper.toBooleanArray( isNullables ); hasSequentialSelects = hasDeferred; + this.sequentialSelectStringsByEntityName = hasSequentialSelects ? new HashMap<>() : Collections.EMPTY_MAP; + // DISCRIMINATOR if ( persistentClass.isPolymorphic() ) {