Skip to content

Commit 8df8fd3

Browse files
committed
HHH-17984 StatelessSession statistics for collections
Signed-off-by: Gavin King <gavin@hibernate.org>
1 parent 6213259 commit 8df8fd3

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ public Object insert(String entityName, Object entity) {
151151
}
152152
persister.setIdentifier( entity, id, this );
153153
forEachOwnedCollection( entity, id, persister,
154-
(descriptor, collection) -> descriptor.recreate( collection, id, this) );
154+
(descriptor, collection) -> {
155+
descriptor.recreate( collection, id, this);
156+
final StatisticsImplementor statistics = getFactory().getStatistics();
157+
if ( statistics.isStatisticsEnabled() ) {
158+
statistics.recreateCollection( descriptor.getRole() );
159+
}
160+
} );
155161
firePostInsert(entity, id, state, persister);
156162
final StatisticsImplementor statistics = getFactory().getStatistics();
157163
if ( statistics.isStatisticsEnabled() ) {
@@ -178,7 +184,13 @@ public void delete(String entityName, Object entity) {
178184
getInterceptor()
179185
.onDelete( entity, id, persister.getPropertyNames(), persister.getPropertyTypes() );
180186
forEachOwnedCollection( entity, id, persister,
181-
(descriptor, collection) -> descriptor.remove(id, this) );
187+
(descriptor, collection) -> {
188+
descriptor.remove( id, this );
189+
final StatisticsImplementor statistics = getFactory().getStatistics();
190+
if ( statistics.isStatisticsEnabled() ) {
191+
statistics.removeCollection( descriptor.getRole() );
192+
}
193+
} );
182194
persister.getDeleteCoordinator().delete( entity, id, version, this );
183195
firePostDelete(entity, id, persister);
184196
final StatisticsImplementor statistics = getFactory().getStatistics();
@@ -223,11 +235,16 @@ public void update(String entityName, Object entity) {
223235
getInterceptor()
224236
.onUpdate( entity, id, state, persister.getPropertyNames(), persister.getPropertyTypes() );
225237
persister.getUpdateCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
226-
// TODO: can we do better here?
227-
forEachOwnedCollection( entity, id, persister,
228-
(descriptor, collection) -> descriptor.remove(id, this) );
229238
forEachOwnedCollection( entity, id, persister,
230-
(descriptor, collection) -> descriptor.recreate( collection, id, this) );
239+
(descriptor, collection) -> {
240+
// TODO: can we do better here?
241+
descriptor.remove( id, this );
242+
descriptor.recreate( collection, id, this );
243+
final StatisticsImplementor statistics = getFactory().getStatistics();
244+
if ( statistics.isStatisticsEnabled() ) {
245+
statistics.updateCollection( descriptor.getRole() );
246+
}
247+
} );
231248
firePostUpdate(entity, id, state, persister);
232249
final StatisticsImplementor statistics = getFactory().getStatistics();
233250
if ( statistics.isStatisticsEnabled() ) {
@@ -247,11 +264,17 @@ public void upsert(String entityName, Object entity) {
247264
.onUpsert( entity, id, state, persister.getPropertyNames(), persister.getPropertyTypes() );
248265
final Object oldVersion = versionToUpsert( entity, persister, state );
249266
persister.getMergeCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
250-
// TODO: can we do better here?
251-
forEachOwnedCollection( entity, id, persister,
252-
(descriptor, collection) -> descriptor.remove(id, this) );
267+
// TODO: statistics for upsert!
253268
forEachOwnedCollection( entity, id, persister,
254-
(descriptor, collection) -> descriptor.recreate( collection, id, this) );
269+
(descriptor, collection) -> {
270+
// TODO: can we do better here?
271+
descriptor.remove( id, this );
272+
descriptor.recreate( collection, id, this );
273+
final StatisticsImplementor statistics = getFactory().getStatistics();
274+
if ( statistics.isStatisticsEnabled() ) {
275+
statistics.updateCollection( descriptor.getRole() );
276+
}
277+
} );
255278
firePostUpsert(entity, id, state, persister);
256279
}
257280
}
@@ -562,11 +585,10 @@ public void initializeCollection(PersistentCollection<?> collection, boolean wri
562585
if ( LOG.isTraceEnabled() ) {
563586
LOG.trace( "Collection initialized" );
564587
}
565-
//TODO: statistics!
566-
// final StatisticsImplementor statistics = getFactory().getStatistics();
567-
// if ( statistics.isStatisticsEnabled() ) {
568-
// statistics.fetchCollection( loadedPersister.getRole() );
569-
// }
588+
final StatisticsImplementor statistics = getFactory().getStatistics();
589+
if ( statistics.isStatisticsEnabled() ) {
590+
statistics.fetchCollection( loadedPersister.getRole() );
591+
}
570592
}
571593
}
572594

@@ -710,6 +732,7 @@ else if ( isPersistentAttributeInterceptable( association ) ) {
710732
proxyInterceptor.setSession( this );
711733
try {
712734
proxyInterceptor.forceInitialize( association, null );
735+
// TODO: statistics?? call statistics.fetchEntity()
713736
}
714737
finally {
715738
proxyInterceptor.unsetSession();
@@ -731,6 +754,10 @@ else if ( association instanceof PersistentCollection ) {
731754
collectionDescriptor.initialize( key, this );
732755
handlePotentiallyEmptyCollection( persistentCollection, getPersistenceContextInternal(), key,
733756
collectionDescriptor );
757+
final StatisticsImplementor statistics = getFactory().getStatistics();
758+
if ( statistics.isStatisticsEnabled() ) {
759+
statistics.fetchCollection( collectionDescriptor.getRole() );
760+
}
734761
}
735762
finally {
736763
persistentCollection.unsetSession( this );

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionStatisticsTest.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.hibernate.orm.test.stateless;
22

33
import jakarta.persistence.Basic;
4+
import jakarta.persistence.ElementCollection;
45
import jakarta.persistence.Entity;
6+
import jakarta.persistence.FetchType;
57
import jakarta.persistence.GeneratedValue;
68
import jakarta.persistence.Id;
79
import org.hibernate.stat.spi.StatisticsImplementor;
@@ -12,7 +14,12 @@
1214
import org.hibernate.testing.orm.junit.Setting;
1315
import org.junit.jupiter.api.Test;
1416

17+
import java.util.ArrayList;
18+
import java.util.List;
19+
1520
import static org.hibernate.cfg.StatisticsSettings.GENERATE_STATISTICS;
21+
import static org.hibernate.graph.GraphSemantic.FETCH;
22+
import static org.hibernate.graph.GraphSemantic.LOAD;
1623
import static org.junit.jupiter.api.Assertions.assertEquals;
1724

1825
@SessionFactory
@@ -28,25 +35,51 @@ void test(SessionFactoryScope scope) {
2835
assertEquals(0, statistics.getEntityLoadCount());
2936
Person person = new Person();
3037
person.name = "Gavin";
38+
person.handles.add("@1ovthafew");
3139
scope.inStatelessTransaction(s -> s.insert(person));
3240
assertEquals(1, statistics.getEntityInsertCount());
41+
assertEquals(1, statistics.getCollectionRecreateCount());
3342
scope.inStatelessSession(s -> s.get(Person.class, person.id));
3443
assertEquals(1, statistics.getEntityLoadCount());
44+
assertEquals(0, statistics.getEntityFetchCount());
45+
assertEquals(1, statistics.getCollectionLoadCount());
46+
assertEquals(0, statistics.getCollectionFetchCount());
3547
person.name = "Gavin King";
3648
scope.inStatelessTransaction(s -> s.update(person));
3749
assertEquals(1, statistics.getEntityUpdateCount());
38-
scope.inStatelessSession(s -> s.get(Person.class, person.id));
50+
assertEquals(1, statistics.getCollectionUpdateCount());
51+
scope.inStatelessSession(s -> s.get(s.createEntityGraph(Person.class), LOAD, person.id));
3952
assertEquals(2, statistics.getEntityLoadCount());
53+
assertEquals(2, statistics.getCollectionLoadCount());
54+
assertEquals(0, statistics.getCollectionFetchCount());
55+
scope.inStatelessSession(s -> s.get(s.createEntityGraph(Person.class), FETCH, person.id));
56+
assertEquals(3, statistics.getEntityLoadCount());
57+
assertEquals(2, statistics.getCollectionLoadCount());
58+
assertEquals(0, statistics.getCollectionFetchCount());
59+
scope.inStatelessSession(s -> s.fetch(s.get(s.createEntityGraph(Person.class), FETCH, person.id).handles));
60+
assertEquals(4, statistics.getEntityLoadCount());
61+
assertEquals(3, statistics.getCollectionLoadCount());
62+
assertEquals(1, statistics.getCollectionFetchCount());
63+
scope.inStatelessSession(s -> s.createQuery("from Person", Person.class).getSingleResult());
64+
assertEquals(5, statistics.getEntityLoadCount());
65+
assertEquals(4, statistics.getCollectionLoadCount());
66+
assertEquals(2, statistics.getCollectionFetchCount());
67+
person.handles.add("hello world");
68+
scope.inStatelessTransaction(s -> s.upsert(person));
69+
assertEquals(2, statistics.getCollectionUpdateCount());
4070
scope.inStatelessTransaction(s -> s.delete(person));
4171
assertEquals(1, statistics.getEntityDeleteCount());
42-
assertEquals(3, statistics.getTransactionCount());
72+
assertEquals(1, statistics.getCollectionRemoveCount());
73+
assertEquals(4, statistics.getTransactionCount());
4374
}
4475

45-
@Entity(name="Entity")
76+
@Entity(name="Person")
4677
static class Person {
4778
@Id @GeneratedValue
4879
long id;
4980
@Basic(optional = false)
5081
String name;
82+
@ElementCollection(fetch = FetchType.EAGER)
83+
List<String> handles = new ArrayList<>();
5184
}
5285
}

0 commit comments

Comments
 (0)