Skip to content

Commit 5a8b2ed

Browse files
karge-itestragavinking
authored andcommitted
HHH-14416 re-persisting removed child entities
1 parent 11139c2 commit 5a8b2ed

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.event;
6+
7+
import org.hibernate.cfg.AvailableSettings;
8+
import org.hibernate.cfg.Configuration;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
11+
import org.junit.Test;
12+
13+
import jakarta.persistence.*;
14+
15+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
16+
17+
public class ReSaveReferencedDeletedEntity extends BaseCoreFunctionalTestCase {
18+
@Override
19+
protected Class<?>[] getAnnotatedClasses() {
20+
return new Class<?>[] { Child.class, Parent.class };
21+
}
22+
23+
@Override
24+
protected void configure(Configuration configuration) {
25+
super.configure( configuration );
26+
configuration.setProperty( AvailableSettings.USE_IDENTIFIER_ROLLBACK, "true" );
27+
}
28+
29+
@Test
30+
@JiraKey("HHH-14416")
31+
public void testReSaveDeletedEntityWithReferences() {
32+
doInHibernate( this::sessionFactory, session -> {
33+
Parent parent = new Parent();
34+
35+
Child child = new Child();
36+
parent.setChild( child );
37+
38+
session.persist( parent );
39+
// session.persist( child );
40+
41+
parent.setChild( null );
42+
session.remove(child);
43+
44+
session.flush();
45+
46+
parent.setChild( child );
47+
session.persist(child);
48+
} );
49+
}
50+
51+
@Test
52+
@JiraKey("HHH-14416")
53+
public void testReSaveDeletedEntityWithReferences2() {
54+
doInHibernate( this::sessionFactory, session -> {
55+
Parent parent = new Parent();
56+
57+
Child child = new Child();
58+
parent.setChild( child );
59+
60+
session.persist( parent );
61+
62+
parent.setChild( null );
63+
session.remove(child);
64+
65+
session.flush();
66+
session.detach(child);
67+
68+
parent.setChild( child );
69+
session.persist(child);
70+
} );
71+
}
72+
73+
@Entity(name = "Child")
74+
public static class Child {
75+
@Id
76+
@GeneratedValue(strategy = GenerationType.IDENTITY)
77+
private Integer id;
78+
79+
public Integer getId() {
80+
return id;
81+
}
82+
83+
public void setId(Integer id) {
84+
this.id = id;
85+
}
86+
}
87+
88+
@Entity(name = "Parent")
89+
public static class Parent {
90+
@Id
91+
@GeneratedValue(strategy = GenerationType.IDENTITY)
92+
private Integer id;
93+
94+
@OneToOne(cascade = CascadeType.ALL)
95+
private Child child;
96+
97+
public Integer getId() {
98+
return id;
99+
}
100+
101+
public void setId(Integer id) {
102+
this.id = id;
103+
}
104+
105+
public Child getChild() {
106+
return child;
107+
}
108+
109+
public void setChild(Child child) {
110+
this.child = child;
111+
}
112+
}
113+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.event;
6+
7+
import org.hibernate.cfg.AvailableSettings;
8+
import org.hibernate.testing.orm.junit.JiraKey;
9+
import org.junit.Test;
10+
11+
import jakarta.persistence.*;
12+
13+
import java.util.Map;
14+
15+
public class ReSaveReferencedDeletedEntityJPA extends org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase {
16+
@Override
17+
protected Class<?>[] getAnnotatedClasses() {
18+
return new Class<?>[] { Child.class, Parent.class };
19+
}
20+
21+
@Override
22+
protected Map<Object, Object> buildSettings() {
23+
Map<Object, Object> settings = super.buildSettings();
24+
settings.put( AvailableSettings.USE_IDENTIFIER_ROLLBACK, "true" );
25+
return settings;
26+
}
27+
28+
@Test
29+
@ JiraKey("HHH-14416")
30+
public void testRefreshUnDeletedEntityWithReferencesJPA() {
31+
EntityManager em = getOrCreateEntityManager();
32+
em.getEntityManagerFactory().getSchemaManager().truncate();
33+
em.getTransaction().begin();
34+
35+
Parent parent = new Parent();
36+
parent.setId(1);
37+
38+
Child child = new Child();
39+
child.setId(2);
40+
parent.setChild( child );
41+
42+
em.persist( parent );
43+
44+
em.flush();
45+
46+
em.remove( parent );
47+
48+
em.flush();
49+
50+
em.detach( parent );
51+
52+
em.persist( parent );
53+
54+
em.flush();
55+
56+
em.refresh( child );
57+
}
58+
59+
@Test
60+
@JiraKey("HHH-14416")
61+
public void testReSaveDeletedEntityWithReferencesJPA() {
62+
EntityManager em = getOrCreateEntityManager();
63+
em.getEntityManagerFactory().getSchemaManager().truncate();
64+
em.getTransaction().begin();
65+
66+
Parent parent = new Parent();
67+
parent.setId(1);
68+
69+
Child child = new Child();
70+
child.setId(2);
71+
parent.setChild( child );
72+
73+
em.persist( parent );
74+
75+
em.remove( child );
76+
77+
em.persist( child );
78+
}
79+
80+
@Entity(name = "Child")
81+
public static class Child {
82+
@Id
83+
private Integer id;
84+
85+
public Integer getId() {
86+
return id;
87+
}
88+
89+
public void setId(Integer id) {
90+
this.id = id;
91+
}
92+
}
93+
94+
@Entity(name = "Parent")
95+
public static class Parent {
96+
@Id
97+
private Integer id;
98+
99+
@OneToOne(cascade = CascadeType.ALL)
100+
private Child child;
101+
102+
public Integer getId() {
103+
return id;
104+
}
105+
106+
public void setId(Integer id) {
107+
this.id = id;
108+
}
109+
110+
public Child getChild() {
111+
return child;
112+
}
113+
114+
public void setChild(Child child) {
115+
this.child = child;
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)