Skip to content

Commit 6c3d700

Browse files
dreab8beikov
authored andcommitted
HHH-15159 Add test for issue
1 parent bc5b866 commit 6c3d700

File tree

2 files changed

+358
-0
lines changed

2 files changed

+358
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.annotations.collectionelement;
8+
9+
import java.util.Arrays;
10+
import java.util.HashSet;
11+
import java.util.List;
12+
import java.util.Set;
13+
14+
import org.hibernate.testing.TestForIssue;
15+
import org.hibernate.testing.orm.junit.DomainModel;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
import jakarta.persistence.CascadeType;
21+
import jakarta.persistence.ElementCollection;
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.FetchType;
24+
import jakarta.persistence.GeneratedValue;
25+
import jakarta.persistence.Id;
26+
import jakarta.persistence.OneToOne;
27+
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
30+
@DomainModel(
31+
annotatedClasses = {
32+
OrphanRemovalOfAnEntityWithElementCollectionTest.Credit.class,
33+
OrphanRemovalOfAnEntityWithElementCollectionTest.Person.class
34+
}
35+
)
36+
@SessionFactory
37+
@TestForIssue(jiraKey = "HHH-15159")
38+
public class OrphanRemovalOfAnEntityWithElementCollectionTest {
39+
40+
@Test
41+
public void testDeleteCredit(SessionFactoryScope scope) {
42+
scope.inTransaction(
43+
session -> {
44+
Credit credit = new Credit();
45+
credit.setReasons( new HashSet<>( Arrays.asList( "one", "two" ) ) );
46+
session.persist( credit );
47+
}
48+
);
49+
50+
scope.inTransaction(
51+
session -> {
52+
Credit credit = session.createQuery( "from Credit", Credit.class ).list().get( 0 );
53+
session.remove( credit );
54+
}
55+
);
56+
}
57+
58+
@Test
59+
public void testRemoveCreditFromPerson(SessionFactoryScope scope) {
60+
final Long firstPersonKey = scope.fromTransaction(
61+
session -> {
62+
Credit credit = new Credit();
63+
credit.setReasons( new HashSet<>( Arrays.asList( "one", "two" ) ) );
64+
65+
Person person = new Person();
66+
person.setName( "William" );
67+
person.addCredit( credit );
68+
69+
session.persist( person );
70+
71+
return person.getId();
72+
}
73+
);
74+
75+
scope.inTransaction(
76+
session -> {
77+
Person person = session.find( Person.class, firstPersonKey );
78+
person.removeCredit();
79+
80+
session.persist( person );
81+
}
82+
);
83+
84+
scope.inTransaction(
85+
session -> {
86+
List<Credit> credits = session.createQuery( "from Credit", Credit.class ).list();
87+
assertThat( credits.size() ).isEqualTo(0);
88+
}
89+
);
90+
}
91+
92+
@Entity(name = "Credit")
93+
public static class Credit {
94+
@Id
95+
@GeneratedValue
96+
private Long id;
97+
98+
@OneToOne(fetch = FetchType.LAZY)
99+
private Person person;
100+
101+
@ElementCollection
102+
private Set<String> reasons;
103+
104+
public Long getId() {
105+
return id;
106+
}
107+
108+
public void setId(Long id) {
109+
this.id = id;
110+
}
111+
112+
public Person getPerson() {
113+
return person;
114+
}
115+
116+
public void setPerson(Person person) {
117+
this.person = person;
118+
}
119+
120+
public Set<String> getReasons() {
121+
return reasons;
122+
}
123+
124+
public void setReasons(Set<String> reasons) {
125+
this.reasons = reasons;
126+
}
127+
}
128+
129+
@Entity(name = "Person")
130+
public static class Person {
131+
@Id
132+
@GeneratedValue
133+
private Long id;
134+
135+
private String name;
136+
137+
@OneToOne(mappedBy = "person", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
138+
private Credit credit;
139+
140+
public Long getId() {
141+
return id;
142+
}
143+
144+
public void setId(Long id) {
145+
this.id = id;
146+
}
147+
148+
public String getName() {
149+
return name;
150+
}
151+
152+
public void setName(String name) {
153+
this.name = name;
154+
}
155+
156+
public Credit getCredit() {
157+
return credit;
158+
}
159+
160+
public void setCredit(Credit credit) {
161+
this.credit = credit;
162+
}
163+
164+
public void addCredit(Credit credit) {
165+
credit.setPerson( this );
166+
this.credit = credit;
167+
}
168+
169+
public void removeCredit() {
170+
this.credit.setPerson( null );
171+
this.credit = null;
172+
}
173+
}
174+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.annotations.collectionelement;
8+
9+
import java.util.Arrays;
10+
import java.util.HashSet;
11+
import java.util.List;
12+
import java.util.Set;
13+
14+
import org.hibernate.cfg.AvailableSettings;
15+
16+
import org.hibernate.testing.TestForIssue;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.ServiceRegistry;
19+
import org.hibernate.testing.orm.junit.SessionFactory;
20+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
21+
import org.hibernate.testing.orm.junit.Setting;
22+
import org.junit.jupiter.api.Test;
23+
24+
import jakarta.persistence.CascadeType;
25+
import jakarta.persistence.ElementCollection;
26+
import jakarta.persistence.Entity;
27+
import jakarta.persistence.FetchType;
28+
import jakarta.persistence.GeneratedValue;
29+
import jakarta.persistence.Id;
30+
import jakarta.persistence.OneToOne;
31+
32+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
33+
34+
@DomainModel(
35+
annotatedClasses = {
36+
OrphanRemovalOfAnEntityWithElementCollectionWithUpdatesAndInsertsOrderTest.Credit.class,
37+
OrphanRemovalOfAnEntityWithElementCollectionWithUpdatesAndInsertsOrderTest.Person.class
38+
}
39+
)
40+
@SessionFactory
41+
@ServiceRegistry(
42+
settings = {
43+
@Setting(name = AvailableSettings.ORDER_INSERTS, value = "true"),
44+
@Setting(name = AvailableSettings.ORDER_UPDATES, value = "true")
45+
}
46+
)
47+
@TestForIssue(jiraKey = "HHH-15159")
48+
public class OrphanRemovalOfAnEntityWithElementCollectionWithUpdatesAndInsertsOrderTest {
49+
50+
@Test
51+
public void testDeleteCredit(SessionFactoryScope scope) {
52+
scope.inTransaction(
53+
session -> {
54+
Credit credit = new Credit();
55+
credit.setReasons( new HashSet<>( Arrays.asList( "one", "two" ) ) );
56+
session.persist( credit );
57+
}
58+
);
59+
60+
scope.inTransaction(
61+
session -> {
62+
Credit credit = session.createQuery( "from Credit", Credit.class ).list().get( 0 );
63+
session.remove( credit );
64+
}
65+
);
66+
}
67+
68+
@Test
69+
public void testRemoveCreditFromPerson(SessionFactoryScope scope) {
70+
final Long firstPersonKey = scope.fromTransaction(
71+
session -> {
72+
Credit credit = new Credit();
73+
credit.setReasons( new HashSet<>( Arrays.asList( "one", "two" ) ) );
74+
75+
Person person = new Person();
76+
person.setName( "William" );
77+
person.addCredit( credit );
78+
79+
session.persist( person );
80+
81+
return person.getId();
82+
}
83+
);
84+
85+
scope.inTransaction(
86+
session -> {
87+
Person person = session.find( Person.class, firstPersonKey );
88+
person.removeCredit();
89+
90+
session.persist( person );
91+
}
92+
);
93+
94+
scope.inTransaction(
95+
session -> {
96+
List<Credit> credits = session.createQuery( "from Credit", Credit.class ).list();
97+
assertThat( credits.size() ).isEqualTo( 0 );
98+
}
99+
);
100+
}
101+
102+
@Entity(name = "Credit")
103+
public static class Credit {
104+
@Id
105+
@GeneratedValue
106+
private Long id;
107+
108+
@OneToOne(fetch = FetchType.LAZY)
109+
private Person person;
110+
111+
@ElementCollection
112+
private Set<String> reasons;
113+
114+
public Long getId() {
115+
return id;
116+
}
117+
118+
public void setId(Long id) {
119+
this.id = id;
120+
}
121+
122+
public Person getPerson() {
123+
return person;
124+
}
125+
126+
public void setPerson(Person person) {
127+
this.person = person;
128+
}
129+
130+
public Set<String> getReasons() {
131+
return reasons;
132+
}
133+
134+
public void setReasons(Set<String> reasons) {
135+
this.reasons = reasons;
136+
}
137+
}
138+
139+
@Entity(name = "Person")
140+
public static class Person {
141+
@Id
142+
@GeneratedValue
143+
private Long id;
144+
145+
private String name;
146+
147+
@OneToOne(mappedBy = "person", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
148+
private Credit credit;
149+
150+
public Long getId() {
151+
return id;
152+
}
153+
154+
public void setId(Long id) {
155+
this.id = id;
156+
}
157+
158+
public String getName() {
159+
return name;
160+
}
161+
162+
public void setName(String name) {
163+
this.name = name;
164+
}
165+
166+
public Credit getCredit() {
167+
return credit;
168+
}
169+
170+
public void setCredit(Credit credit) {
171+
this.credit = credit;
172+
}
173+
174+
public void addCredit(Credit credit) {
175+
credit.setPerson( this );
176+
this.credit = credit;
177+
}
178+
179+
public void removeCredit() {
180+
this.credit.setPerson( null );
181+
this.credit = null;
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)