From 41d9d82f24cde9e74c7aafe702fe5045bd1ca9d9 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Wed, 24 May 2023 16:33:11 +0200 Subject: [PATCH] HHH-16649 - This commit effectively reverts d8bf6499 (HHH-15910), in which static remove methods were added to the Hibernate class The reason for this is that the methods in fact don't work, since the machinery behind them just isn't there, nor has it even been. The CollectionSizeTest that was modified to test these new methods was incomplete and hid this fact. Signed-off-by: Jan Schatteman --- .../main/java/org/hibernate/Hibernate.java | 51 ------------------- .../hibernate/annotations/LazyCollection.java | 5 +- .../annotations/LazyCollectionOption.java | 5 +- .../spi/AbstractPersistentCollection.java | 4 +- .../collection/spi/PersistentBag.java | 21 -------- .../collection/spi/PersistentList.java | 21 -------- .../collection/spi/PersistentMap.java | 22 -------- .../collection/spi/PersistentSet.java | 21 -------- .../collection/basic/CollectionSizeTest.java | 20 -------- .../orm/test/collection/basic/Contact.java | 14 ----- 10 files changed, 6 insertions(+), 178 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Hibernate.java b/hibernate-core/src/main/java/org/hibernate/Hibernate.java index 3cbbac50ff..b945f79474 100644 --- a/hibernate-core/src/main/java/org/hibernate/Hibernate.java +++ b/hibernate-core/src/main/java/org/hibernate/Hibernate.java @@ -233,57 +233,6 @@ public final class Hibernate { : list.get(key); } - /** - * Remove the value associated with the given key by the given persistent - * map, without fetching the state of the map from the database. - * - * @param map a persistent map associated with an open session - * @param key a key belonging to the map - * @return the previous value associated by the map with the given key, or null if there was no value associated - * with the given key - * - * @since 6.2.0 - */ - public static V remove(Map map, K key) { - return ( map instanceof PersistentMap ) - ? ( (PersistentMap) map ).queuedRemove( key ) - : map.remove( key ); - } - - /** - * Remove the specified element of the given persistent set, - * without fetching the state of the set from the database. - * - * @param set a persistent set associated with an open session - * @param element an element belonging to the set - * @return true if this set contained the specified element - * - * @since 6.2.0 - */ - public static boolean remove(Set set, T element) { - return ( set instanceof PersistentSet ) - ? ( (PersistentSet) set ).queuedRemove( element ) - : set.remove( element ); - } - - /** - * Remove the specified element of the given persistent list, - * without fetching the state of the list from the database. - * - * @param list a persistent list associated with an open session - * @param element an element belonging to the list - * @return true if this list contained the specified element - * - * @since 6.2.0 - */ - public static boolean remove(List list, T element) { - return ( list instanceof PersistentList ) - ? ( (PersistentList) list ).queuedRemove( element ) - : ( list instanceof PersistentBag ) - ? ( (PersistentBag) list ).queuedRemove( element ) - : list.remove( element ); - } - /** * Get the true, underlying class of a proxied entity. This operation will * initialize a proxy by side effect. diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollection.java b/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollection.java index 43499c29ef..3a3900c097 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollection.java @@ -31,9 +31,8 @@ import java.util.Map; * instead of {@code LazyCollection(FALSE)}. *
  • Use static methods of {@link org.hibernate.Hibernate}, * for example {@link org.hibernate.Hibernate#size(Collection)}, - * {@link org.hibernate.Hibernate#contains(Collection, Object)}, - * {@link org.hibernate.Hibernate#get(Map, Object)}, or - * {@link org.hibernate.Hibernate#remove(Map, Object)} instead + * {@link org.hibernate.Hibernate#contains(Collection, Object)}, or + * {@link org.hibernate.Hibernate#get(Map, Object)} instead * of {@code LazyCollection(EXTRA)}. * */ diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollectionOption.java b/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollectionOption.java index a310dfa0bf..c4983bf5a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollectionOption.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/LazyCollectionOption.java @@ -25,9 +25,8 @@ import java.util.Map; * instead of {@code LazyCollection(FALSE)}. *
  • Use static methods of {@link org.hibernate.Hibernate}, * for example {@link org.hibernate.Hibernate#size(Collection)}, - * {@link org.hibernate.Hibernate#contains(Collection, Object)}, - * {@link org.hibernate.Hibernate#get(Map, Object)}, or - * {@link org.hibernate.Hibernate#remove(Map, Object)} instead + * {@link org.hibernate.Hibernate#contains(Collection, Object)}, or + * {@link org.hibernate.Hibernate#get(Map, Object)} instead * of {@code LazyCollection(EXTRA)}. * */ 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 ce88e92539..7af7ec63ac 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 @@ -620,7 +620,7 @@ public abstract class AbstractPersistentCollection implements Serializable, P ); } - void throwLazyInitializationExceptionIfNotConnected() { + private void throwLazyInitializationExceptionIfNotConnected() { if ( !isConnectedToSession() ) { throwLazyInitializationException( "no session or session was closed" ); } @@ -629,7 +629,7 @@ public abstract class AbstractPersistentCollection implements Serializable, P } } - void throwLazyInitializationException(String message) { + private void throwLazyInitializationException(String message) { throw new LazyInitializationException( "failed to lazily initialize a collection" + (role == null ? "" : " of role: " + role) + diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java index 7d6e7a4a29..fbd00a295f 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java @@ -409,27 +409,6 @@ public class PersistentBag extends AbstractPersistentCollection implements } } - @Internal - public boolean queuedRemove(Object element) { - final CollectionEntry entry = getSession().getPersistenceContextInternal().getCollectionEntry( PersistentBag.this ); - if ( entry == null ) { - throwLazyInitializationExceptionIfNotConnected(); - throwLazyInitializationException("collection not associated with session"); - } - else { - final CollectionPersister persister = entry.getLoadedPersister(); - if ( hasQueuedOperations() ) { - getSession().flush(); - } - if ( persister.elementExists( entry.getLoadedKey(), element, getSession() ) ) { - elementRemoved = true; - queueOperation( new PersistentBag.SimpleRemove( (E) element ) ); - return true; - } - } - return false; - } - @Override public boolean containsAll(Collection c) { read(); diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentList.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentList.java index d9fa3ade10..c662487175 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentList.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentList.java @@ -221,27 +221,6 @@ public class PersistentList extends AbstractPersistentCollection implement } } - @Internal - public boolean queuedRemove(Object element) { - final CollectionEntry entry = getSession().getPersistenceContextInternal().getCollectionEntry( PersistentList.this ); - if ( entry == null ) { - throwLazyInitializationExceptionIfNotConnected(); - throwLazyInitializationException("collection not associated with session"); - } - else { - final CollectionPersister persister = entry.getLoadedPersister(); - if ( hasQueuedOperations() ) { - getSession().flush(); - } - if ( persister.elementExists( entry.getLoadedKey(), element, getSession() ) ) { - elementRemoved = true; - queueOperation( new PersistentList.SimpleRemove( (E) element ) ); - return true; - } - } - return false; - } - @Override public boolean containsAll(Collection coll) { read(); 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 c5f77fb03a..b33453b86e 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 @@ -197,28 +197,6 @@ public class PersistentMap extends AbstractPersistentCollection implemen return map.remove( key ); } - @Internal - public E queuedRemove(Object key) { - final CollectionEntry entry = getSession().getPersistenceContextInternal().getCollectionEntry( PersistentMap.this ); - if ( entry == null ) { - throwLazyInitializationExceptionIfNotConnected(); - throwLazyInitializationException("collection not associated with session"); - } - else { - final CollectionPersister persister = entry.getLoadedPersister(); - if ( hasQueuedOperations() ) { - getSession().flush(); - } - Object element = persister.getElementByIndex( entry.getLoadedKey(), key, getSession(), getOwner() ); - if ( element != null ) { - elementRemoved = true; - queueOperation( new Remove( (K) key, (E) element ) ); - return (E) element; - } - } - return null; - } - @Override public void putAll(Map puts) { if ( puts.size() > 0 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentSet.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentSet.java index 00bb9e874d..a8075e7329 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentSet.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentSet.java @@ -228,27 +228,6 @@ public class PersistentSet extends AbstractPersistentCollection implements } } - @Internal - public boolean queuedRemove(Object element) { - final CollectionEntry entry = getSession().getPersistenceContextInternal().getCollectionEntry( PersistentSet.this ); - if ( entry == null ) { - throwLazyInitializationExceptionIfNotConnected(); - throwLazyInitializationException("collection not associated with session"); - } - else { - final CollectionPersister persister = entry.getLoadedPersister(); - if ( hasQueuedOperations() ) { - getSession().flush(); - } - if ( persister.elementExists( entry.getLoadedKey(), element, getSession() ) ) { - elementRemoved = true; - queueOperation( new SimpleRemove( (E) element ) ); - return true; - } - } - return false; - } - @Override public boolean containsAll(Collection coll) { read(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/CollectionSizeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/CollectionSizeTest.java index 9386fb7a1e..cae86a5ab9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/CollectionSizeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/CollectionSizeTest.java @@ -12,9 +12,7 @@ import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -37,16 +35,12 @@ public class CollectionSizeTest { emailAddresses.add( new EmailAddress( "test1@test.com" ) ); emailAddresses.add( new EmailAddress( "test2@test.com" ) ); emailAddresses.add( new EmailAddress( "test3@test.com" ) ); - List emailAddresses3 = new ArrayList<>(); - emailAddresses3.add( new EmailAddress( "test4@test.com" ) ); - emailAddresses3.add( new EmailAddress( "test5@test.com" ) ); User user = new User(); user.setName( "john" ); Contact contact = new Contact(); contact.setName( "John Doe" ); contact.setEmailAddresses( emailAddresses ); emailAddresses.forEach( address -> contact.getContactsByEmail().put(address, contact) ); - contact.setEmailAddresses3( emailAddresses3 ); scope.inTransaction( session -> { @@ -70,20 +64,6 @@ public class CollectionSizeTest { assertEquals( cont, Hibernate.get(contactsByEmail, new EmailAddress( "test1@test.com" ) ) ); assertNull( Hibernate.get(contactsByEmail, new EmailAddress( "test9@test.com" ) ) ); assertFalse( Hibernate.isInitialized(contactsByEmail) ); - - Hibernate.remove( contactsByEmail, new EmailAddress( "test1@test.com" ) ); - assertFalse( Hibernate.isInitialized(contactsByEmail) ); - assertEquals( cont, Hibernate.get(contactsByEmail, new EmailAddress( "test1@test.com" ) ) ); - assertEquals( Hibernate.size( contactsByEmail.entrySet() ), 3 ); - - Hibernate.remove( addresses, new EmailAddress( "test1@test.com" ) ); - assertFalse( Hibernate.isInitialized(addresses) ); - assertEquals( Hibernate.size( addresses ), 3 ); - - List addresses3 = cont.getEmailAddresses3(); - Hibernate.remove( addresses3, new EmailAddress( "test4@test.com" ) ); - assertFalse( Hibernate.isInitialized( addresses3 ) ); - assertEquals( Hibernate.size(addresses3), 2 ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/Contact.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/Contact.java index 16cdaf99b6..79d1b35738 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/Contact.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/basic/Contact.java @@ -7,10 +7,8 @@ package org.hibernate.orm.test.collection.basic; import java.io.Serializable; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import jakarta.persistence.Basic; @@ -33,8 +31,6 @@ public class Contact implements Serializable { private String name; private Set emailAddresses = new HashSet<>(); private Set emailAddresses2 = new HashSet<>(); - - private List emailAddresses3 = new ArrayList<>(); private Map contactsByEmail = new HashMap<>(); @Id @@ -56,16 +52,6 @@ public class Contact implements Serializable { this.name = name; } - @ElementCollection - @CollectionTable(name = "user_email_addresses3", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")) - public List getEmailAddresses3() { - return emailAddresses3; - } - - public void setEmailAddresses3(List emailAddresses3) { - this.emailAddresses3 = emailAddresses3; - } - @ElementCollection @CollectionTable(name = "user_email_addresses2", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")) public Set getEmailAddresses2() {