| 
 | 1 | +/*  | 
 | 2 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 3 | + * Copyright Red Hat Inc. and Hibernate Authors  | 
 | 4 | + */  | 
 | 5 | +package org.hibernate.orm.test.jpa.emops;  | 
 | 6 | + | 
 | 7 | +import jakarta.persistence.ElementCollection;  | 
 | 8 | +import jakarta.persistence.Entity;  | 
 | 9 | +import jakarta.persistence.Id;  | 
 | 10 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;  | 
 | 11 | +import org.hibernate.testing.orm.junit.Jpa;  | 
 | 12 | +import org.junit.jupiter.api.Test;  | 
 | 13 | + | 
 | 14 | +import java.util.HashSet;  | 
 | 15 | +import java.util.Set;  | 
 | 16 | + | 
 | 17 | +import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 18 | + | 
 | 19 | +@Jpa(annotatedClasses = MergeNullCollectionTest.Thing.class)  | 
 | 20 | +class MergeNullCollectionTest {  | 
 | 21 | +	@Test void test(EntityManagerFactoryScope scope) {  | 
 | 22 | +		Thing thing = new Thing();  | 
 | 23 | +		thing.strings.add( "hello" );  | 
 | 24 | +		thing.strings.add( "goodbye" );  | 
 | 25 | +		scope.inTransaction( em -> {  | 
 | 26 | +			em.persist( thing );  | 
 | 27 | +		} );  | 
 | 28 | +		scope.inTransaction( em -> {  | 
 | 29 | +			assertEquals(2, em.find( Thing.class, thing.id ).strings.size());  | 
 | 30 | +		});  | 
 | 31 | +		thing.strings = null;  | 
 | 32 | +		scope.inTransaction( em -> {  | 
 | 33 | +			assertEquals( 0, em.merge( thing ).strings.size() );  | 
 | 34 | +		} );  | 
 | 35 | +		scope.inTransaction( em -> {  | 
 | 36 | +			assertEquals(0, em.find( Thing.class, thing.id ).strings.size());  | 
 | 37 | +		});  | 
 | 38 | +	}  | 
 | 39 | +	@Entity  | 
 | 40 | +	static class Thing {  | 
 | 41 | +		@Id  | 
 | 42 | +		long id;  | 
 | 43 | +		@ElementCollection  | 
 | 44 | +		Set<String> strings = new HashSet<>();  | 
 | 45 | +	}  | 
 | 46 | +}  | 
0 commit comments