From 69735f876180a0969185a37cf329efdfdb841053 Mon Sep 17 00:00:00 2001 From: gtoison Date: Tue, 27 May 2025 13:26:37 +0200 Subject: [PATCH 1/2] HHH-18885 Introduce DelayedOperation.getAddedEntry() for maps Extra lazy maps need to persist the added entry (key and value), not the added value. Let PersistentMap.AbstractMapValueDelayedOperation return the added entry so OneToManyPersister.writeIndex() can execute the queued operation. --- .../collection/spi/AbstractPersistentCollection.java | 6 +++++- .../java/org/hibernate/collection/spi/PersistentMap.java | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java index 5342e5c7f978..d6f707ebadb8 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java @@ -852,7 +852,7 @@ public final Iterator queuedAdditionIterator() { @Override public E next() { - return operationQueue.get( index++ ).getAddedInstance(); + return operationQueue.get( index++ ).getAddedEntry(); } @Override @@ -1226,6 +1226,10 @@ protected interface DelayedOperation { void operate(); E getAddedInstance(); + + default E getAddedEntry() { + return getAddedInstance(); + } E getOrphan(); } diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java index 62711cba5b2b..da28cbaeebf9 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java @@ -526,6 +526,12 @@ protected AbstractMapValueDelayedOperation(K index, E addedValue, E orphan) { protected final K getIndex() { return index; } + + @Override + public E getAddedEntry() { + // The (E) cast is very hacky because E is not Map.Entry but we need it to conform to PersistentCollection.queuedAdditionIterator() + return (E) Map.entry( getIndex(), getAddedInstance() ); + } } final class Put extends AbstractMapValueDelayedOperation { From aec8baacc02c277bd15cf98673ec98b500fea7a1 Mon Sep 17 00:00:00 2001 From: gtoison Date: Tue, 27 May 2025 14:59:00 +0200 Subject: [PATCH 2/2] HHH-18885 let queuedAdditionIterator() return Iterator For maps the iterator operates over a collection of Map.Entry --- .../collection/spi/AbstractPersistentCollection.java | 8 ++++---- .../hibernate/collection/spi/PersistentCollection.java | 2 +- .../java/org/hibernate/collection/spi/PersistentMap.java | 7 +++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java index d6f707ebadb8..72b810ddaf22 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java @@ -845,13 +845,13 @@ public final boolean hasQueuedOperations() { } @Override - public final Iterator queuedAdditionIterator() { + public final Iterator queuedAdditionIterator() { if ( hasQueuedOperations() ) { return new Iterator<>() { private int index; @Override - public E next() { + public Object next() { return operationQueue.get( index++ ).getAddedEntry(); } @@ -1226,8 +1226,8 @@ protected interface DelayedOperation { void operate(); E getAddedInstance(); - - default E getAddedEntry() { + + default Object getAddedEntry() { return getAddedInstance(); } diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java index 20895284b97a..50174696bae1 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java @@ -388,7 +388,7 @@ default boolean needsUpdating( * * @return The iterator */ - Iterator queuedAdditionIterator(); + Iterator queuedAdditionIterator(); /** * Get the "queued" orphans diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java index da28cbaeebf9..9f1e713269e2 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentMap.java @@ -526,11 +526,10 @@ protected AbstractMapValueDelayedOperation(K index, E addedValue, E orphan) { protected final K getIndex() { return index; } - + @Override - public E getAddedEntry() { - // The (E) cast is very hacky because E is not Map.Entry but we need it to conform to PersistentCollection.queuedAdditionIterator() - return (E) Map.entry( getIndex(), getAddedInstance() ); + public Object getAddedEntry() { + return Map.entry( getIndex(), getAddedInstance() ); } }