Skip to content

Commit f2a73c9

Browse files
dreab8beikov
authored andcommitted
HHH-13815 Add test for issue
1 parent 4667259 commit f2a73c9

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.merge;
6+
7+
import org.hibernate.testing.orm.junit.JiraKey;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import jakarta.persistence.CascadeType;
12+
import jakarta.persistence.Entity;
13+
import jakarta.persistence.FetchType;
14+
import jakarta.persistence.GeneratedValue;
15+
import jakarta.persistence.GenerationType;
16+
import jakarta.persistence.Id;
17+
import jakarta.persistence.JoinColumn;
18+
import jakarta.persistence.ManyToOne;
19+
import jakarta.persistence.OneToMany;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
24+
25+
/**
26+
* @author Lisandro Fernandez (kelechul at gmail dot com)
27+
*/
28+
@JiraKey("HHH-13815")
29+
public class BidirectionalOneToManyMergeTest extends org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase {
30+
31+
@Override
32+
protected Class<?>[] getAnnotatedClasses() {
33+
return new Class<?>[]{
34+
Post.class,
35+
PostComment.class,
36+
};
37+
}
38+
39+
@Before
40+
public void setUp() {
41+
doInJPA(this::entityManagerFactory, entityManager -> {
42+
entityManager.persist(
43+
new Post("High-Performance Java Persistence").setId(1L)
44+
);
45+
});
46+
}
47+
48+
@Test
49+
public void testMerge() {
50+
doInJPA(this::entityManagerFactory, entityManager -> {
51+
Post post = entityManager.find(Post.class, 1L);
52+
post.addComment(new PostComment("This post rocks!", post));
53+
post.getComments().isEmpty();
54+
entityManager.merge(post);
55+
});
56+
}
57+
58+
@Entity
59+
public static class Post {
60+
61+
@Id
62+
private Long id;
63+
64+
private String title;
65+
66+
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
67+
private List<PostComment> comments = new ArrayList<>();
68+
69+
public Post() {
70+
}
71+
72+
public Post(String title) {
73+
this.title = title;
74+
}
75+
76+
public Long getId() {
77+
return id;
78+
}
79+
80+
public Post setId(Long id) {
81+
this.id = id;
82+
return this;
83+
}
84+
85+
public String getTitle() {
86+
return title;
87+
}
88+
89+
public Post setTitle(String title) {
90+
this.title = title;
91+
return this;
92+
}
93+
94+
public List<PostComment> getComments() {
95+
return comments;
96+
}
97+
98+
private Post setComments(List<PostComment> comments) {
99+
this.comments = comments;
100+
return this;
101+
}
102+
103+
public Post addComment(PostComment comment) {
104+
comments.add(comment);
105+
comment.setPost(this);
106+
107+
return this;
108+
}
109+
110+
public Post removeComment(PostComment comment) {
111+
comments.remove(comment);
112+
comment.setPost(null);
113+
114+
return this;
115+
}
116+
}
117+
118+
@Entity
119+
public static class PostComment {
120+
121+
@Id
122+
@GeneratedValue(strategy = GenerationType.IDENTITY)
123+
private Long id;
124+
125+
private String review;
126+
127+
@ManyToOne(fetch = FetchType.LAZY)
128+
@JoinColumn(name = "post_id")
129+
private Post post;
130+
131+
public PostComment() {
132+
}
133+
134+
public PostComment(String review, Post post) {
135+
this.review = review;
136+
this.post = post;
137+
}
138+
139+
public Long getId() {
140+
return id;
141+
}
142+
143+
public PostComment setId(Long id) {
144+
this.id = id;
145+
return this;
146+
}
147+
148+
public String getReview() {
149+
return review;
150+
}
151+
152+
public PostComment setReview(String review) {
153+
this.review = review;
154+
return this;
155+
}
156+
157+
public Post getPost() {
158+
return post;
159+
}
160+
161+
public PostComment setPost(Post post) {
162+
this.post = post;
163+
return this;
164+
}
165+
166+
@Override
167+
public boolean equals(Object o) {
168+
if (this == o) return true;
169+
if (!(o instanceof PostComment)) return false;
170+
return id != null && id.equals(((PostComment) o).getId());
171+
}
172+
173+
@Override
174+
public int hashCode() {
175+
return 31;
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)