Skip to content

Commit 49f846b

Browse files
committed
HHH-19535 test to demo relevant capabilities of new preMerge() interceptor
1 parent fb6087e commit 49f846b

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 org.hibernate.Hibernate;
8+
import org.hibernate.Interceptor;
9+
import org.hibernate.type.Type;
10+
11+
/**
12+
* An interceptor that initializes null collection references
13+
* when an entity is passed to persist() or merge().
14+
*
15+
* @author Gavin King
16+
*/
17+
public class MergeInterceptor implements Interceptor {
18+
@Override
19+
public boolean onPersist(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) {
20+
boolean result = false;
21+
for ( int i = 0; i < types.length; i++ ) {
22+
if ( types[i].isCollectionType() && state[i] == null ) {
23+
state[i] = Hibernate.set().createNewInstance();
24+
result = true;
25+
}
26+
}
27+
return result;
28+
}
29+
30+
@Override
31+
public void preMerge(Object entity, Object[] state, String[] propertyNames, Type[] types) {
32+
for ( int i = 0; i < types.length; i++ ) {
33+
if ( types[i].isCollectionType() && state[i] == null ) {
34+
state[i] = Hibernate.set().createDetachedInstance();
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)