Skip to content

Commit 3a86b9b

Browse files
committed
HHH-14502 Iterations and memory retention improvements for processing of PropertyContainer metadata
1 parent 9412c44 commit 3a86b9b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,7 @@ static int addElementsOfClass(
15021502
MetadataBuildingContext context) {
15031503
int idPropertyCounter = 0;
15041504

1505-
Collection<XProperty> properties = propertyContainer.getProperties();
1506-
for ( XProperty p : properties ) {
1505+
for ( XProperty p : propertyContainer.propertyIterator() ) {
15071506
final int currentIdPropertyCounter = addProperty(
15081507
propertyContainer,
15091508
p,

hibernate-core/src/main/java/org/hibernate/cfg/PropertyContainer.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.hibernate.cfg;
1111

12+
import java.util.ArrayList;
1213
import java.util.Collection;
1314
import java.util.Collections;
1415
import java.util.HashMap;
@@ -35,6 +36,7 @@
3536
import org.hibernate.cfg.annotations.HCANNHelper;
3637
import org.hibernate.internal.CoreMessageLogger;
3738
import org.hibernate.internal.util.StringHelper;
39+
import org.hibernate.internal.util.collections.CollectionHelper;
3840

3941
import org.jboss.logging.Logger;
4042

@@ -63,7 +65,7 @@ class PropertyContainer {
6365
*/
6466
private final AccessType classLevelAccessType;
6567

66-
private final TreeMap<String, XProperty> persistentAttributeMap;
68+
private final List<XProperty> persistentAttributes;
6769

6870
PropertyContainer(XClass clazz, XClass entityAtStake, AccessType defaultClassLevelAccessType) {
6971
this.xClass = clazz;
@@ -83,7 +85,6 @@ class PropertyContainer {
8385
: defaultClassLevelAccessType;
8486
assert classLevelAccessType == AccessType.FIELD || classLevelAccessType == AccessType.PROPERTY;
8587

86-
this.persistentAttributeMap = new TreeMap<String, XProperty>();
8788

8889
final List<XProperty> fields = xClass.getDeclaredProperties( AccessType.FIELD.getType() );
8990
final List<XProperty> getters = xClass.getDeclaredProperties( AccessType.PROPERTY.getType() );
@@ -92,18 +93,23 @@ class PropertyContainer {
9293

9394
final Map<String,XProperty> persistentAttributesFromGetters = new HashMap<String, XProperty>();
9495

96+
final TreeMap<String, XProperty> localAttributeMap = new TreeMap<>();
9597
collectPersistentAttributesUsingLocalAccessType(
96-
persistentAttributeMap,
98+
xClass,
99+
localAttributeMap,
97100
persistentAttributesFromGetters,
98101
fields,
99102
getters
100103
);
101104
collectPersistentAttributesUsingClassLevelAccessType(
102-
persistentAttributeMap,
105+
xClass,
106+
classLevelAccessType,
107+
localAttributeMap,
103108
persistentAttributesFromGetters,
104109
fields,
105110
getters
106111
);
112+
this.persistentAttributes = verifyAndInitializePersistentAttributes( xClass, localAttributeMap );
107113
}
108114

109115
private void preFilter(List<XProperty> fields, List<XProperty> getters) {
@@ -124,7 +130,8 @@ private void preFilter(List<XProperty> fields, List<XProperty> getters) {
124130
}
125131
}
126132

127-
private void collectPersistentAttributesUsingLocalAccessType(
133+
private static void collectPersistentAttributesUsingLocalAccessType(
134+
XClass xClass,
128135
TreeMap<String, XProperty> persistentAttributeMap,
129136
Map<String,XProperty> persistentAttributesFromGetters,
130137
List<XProperty> fields,
@@ -176,7 +183,9 @@ private void collectPersistentAttributesUsingLocalAccessType(
176183
}
177184
}
178185

179-
private void collectPersistentAttributesUsingClassLevelAccessType(
186+
private static void collectPersistentAttributesUsingClassLevelAccessType(
187+
XClass xClass,
188+
AccessType classLevelAccessType,
180189
TreeMap<String, XProperty> persistentAttributeMap,
181190
Map<String,XProperty> persistentAttributesFromGetters,
182191
List<XProperty> fields,
@@ -229,20 +238,30 @@ public AccessType getClassLevelAccessType() {
229238
return classLevelAccessType;
230239
}
231240

241+
/**
242+
* @deprecated Use the {@link #propertyIterator()} method instead.
243+
*/
244+
@Deprecated
232245
public Collection<XProperty> getProperties() {
233-
assertTypesAreResolvable();
234-
return Collections.unmodifiableCollection( persistentAttributeMap.values() );
246+
return Collections.unmodifiableCollection( this.persistentAttributes );
247+
}
248+
249+
public Iterable<XProperty> propertyIterator() {
250+
return persistentAttributes;
235251
}
236252

237-
private void assertTypesAreResolvable() {
238-
for ( XProperty xProperty : persistentAttributeMap.values() ) {
253+
private static List<XProperty> verifyAndInitializePersistentAttributes(XClass xClass, Map<String, XProperty> localAttributeMap) {
254+
ArrayList<XProperty> output = new ArrayList( localAttributeMap.size() );
255+
for ( XProperty xProperty : localAttributeMap.values() ) {
239256
if ( !xProperty.isTypeResolved() && !discoverTypeWithoutReflection( xProperty ) ) {
240257
String msg = "Property " + StringHelper.qualify( xClass.getName(), xProperty.getName() ) +
241258
" has an unbound type and no explicit target entity. Resolve this Generic usage issue" +
242259
" or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type";
243260
throw new AnnotationException( msg );
244261
}
262+
output.add( xProperty );
245263
}
264+
return CollectionHelper.toSmallList( output );
246265
}
247266
//
248267
// private void considerExplicitFieldAndPropertyAccess() {

0 commit comments

Comments
 (0)