| 
 | 1 | +/*  | 
 | 2 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 3 | + * Copyright Red Hat Inc. and Hibernate Authors  | 
 | 4 | + */  | 
 | 5 | +package org.hibernate.orm.test.interceptor.merge;  | 
 | 6 | + | 
 | 7 | +import jakarta.persistence.ElementCollection;  | 
 | 8 | +import jakarta.persistence.Entity;  | 
 | 9 | +import jakarta.persistence.GeneratedValue;  | 
 | 10 | +import jakarta.persistence.Id;  | 
 | 11 | +import org.hibernate.Hibernate;  | 
 | 12 | +import org.hibernate.cfg.SessionEventSettings;  | 
 | 13 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;  | 
 | 14 | +import org.hibernate.testing.orm.junit.Jpa;  | 
 | 15 | +import org.hibernate.testing.orm.junit.Setting;  | 
 | 16 | +import org.junit.jupiter.api.Test;  | 
 | 17 | + | 
 | 18 | +import java.util.Set;  | 
 | 19 | + | 
 | 20 | +import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 21 | +import static org.junit.jupiter.api.Assertions.assertFalse;  | 
 | 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull;  | 
 | 23 | +import static org.junit.jupiter.api.Assertions.assertNull;  | 
 | 24 | +import static org.junit.jupiter.api.Assertions.assertTrue;  | 
 | 25 | + | 
 | 26 | +@Jpa(annotatedClasses = MergeInterceptionTest.Thing.class,  | 
 | 27 | +	integrationSettings = @Setting(name = SessionEventSettings.INTERCEPTOR,  | 
 | 28 | +			value = "org.hibernate.orm.test.interceptor.merge.MergeInterceptor"))  | 
 | 29 | +class MergeInterceptionTest {  | 
 | 30 | +	@Test  | 
 | 31 | +	void test(EntityManagerFactoryScope scope) {  | 
 | 32 | +		Thing t = scope.fromTransaction( em -> {  | 
 | 33 | +			Thing thing = new Thing();  | 
 | 34 | +			thing.name = "Hibernate";  | 
 | 35 | +			assertNull( thing.names );  | 
 | 36 | +			em.persist( thing );  | 
 | 37 | +			assertNotNull( thing.names );  | 
 | 38 | +			assertTrue( Hibernate.isInitialized( thing.names ) );  | 
 | 39 | +			assertEquals( 0, thing.names.size() );  | 
 | 40 | +			thing.names.add( "CDI" );  | 
 | 41 | +			thing.names.add( "Ceylon" );  | 
 | 42 | +			return thing;  | 
 | 43 | +		} );  | 
 | 44 | +		scope.inTransaction( em -> {  | 
 | 45 | +			t.name = "Hibernate ORM";  | 
 | 46 | +			t.names = null;  | 
 | 47 | +			Thing thing = em.merge( t );  | 
 | 48 | +			assertNull( t.names );  | 
 | 49 | +			assertNotNull( thing.names );  | 
 | 50 | +			assertFalse( Hibernate.isInitialized( thing.names ) );  | 
 | 51 | +			assertEquals( 2, thing.names.size() );  | 
 | 52 | +		} );  | 
 | 53 | +	}  | 
 | 54 | +	@Entity  | 
 | 55 | +	static class Thing {  | 
 | 56 | +		@Id @GeneratedValue  | 
 | 57 | +		private Long id;  | 
 | 58 | +		private String name;  | 
 | 59 | +		@ElementCollection  | 
 | 60 | +		Set<String> names;  | 
 | 61 | +	}  | 
 | 62 | +}  | 
0 commit comments