Skip to content

Commit c5f6264

Browse files
committed
Add insertMultiple, updateMultiple, and deleteMultiple test cases
1 parent 5aedffd commit c5f6264

1 file changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
package com.vladmihalcea.hpjp.hibernate.batch;
2+
3+
import com.vladmihalcea.hpjp.util.AbstractTest;
4+
import com.vladmihalcea.hpjp.util.providers.Database;
5+
import io.hypersistence.utils.hibernate.id.SequenceOptimizer;
6+
import jakarta.persistence.*;
7+
import jakarta.persistence.criteria.Root;
8+
import org.hibernate.cfg.AvailableSettings;
9+
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
10+
import org.hibernate.query.criteria.JpaCriteriaDelete;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Properties;
16+
import java.util.stream.IntStream;
17+
import java.util.stream.LongStream;
18+
19+
/**
20+
* BatchingTest - Test to check the JDBC batch support
21+
*
22+
* @author Vlad Mihalcea
23+
*/
24+
public class StatelessSessionBatchingMultipleTest extends AbstractTest {
25+
26+
@Override
27+
protected Class<?>[] entities() {
28+
return new Class<?>[]{
29+
Post.class,
30+
PostComment.class
31+
};
32+
}
33+
34+
private static final int POST_COUNT = 3;
35+
36+
@Override
37+
protected Database database() {
38+
return Database.POSTGRESQL;
39+
}
40+
41+
@Test
42+
public void testInsertMultiplePosts() {
43+
LOGGER.info("testInsertMultiplePosts");
44+
45+
doInStatelessSession(session -> {
46+
session.insertMultiple(
47+
LongStream.rangeClosed(1, POST_COUNT).boxed()
48+
.map( i -> new Post()
49+
.setId(i)
50+
.setTitle(String.format("Post no. %d", i)))
51+
.toList()
52+
);
53+
});
54+
}
55+
56+
@Test
57+
public void testInsertMultiplePostsAndComments() {
58+
LOGGER.info("testInsertPostsAndComments");
59+
60+
doInStatelessSession(session -> {
61+
List<Post> posts = LongStream.rangeClosed(1, POST_COUNT).boxed()
62+
.map(i -> new Post()
63+
.setId(i)
64+
.setTitle(String.format("Post no. %d", i)))
65+
.toList();
66+
67+
final int COMMENT_COUNT = 5;
68+
69+
final List<PostComment> postComments = posts.stream()
70+
.flatMap(post ->
71+
LongStream.rangeClosed(1, COMMENT_COUNT).boxed()
72+
.map(i -> new PostComment()
73+
.setPost(post)
74+
.setReview(
75+
String.format(
76+
"Post comment no. %d",
77+
(post.getId() - 1) * COMMENT_COUNT + i
78+
)
79+
))
80+
).toList();
81+
82+
session.insertMultiple(posts);
83+
session.insertMultiple(postComments);
84+
});
85+
}
86+
87+
@Test
88+
public void testUpdateMultiplePosts() {
89+
LOGGER.info("testUpdatePosts");
90+
insertPosts();
91+
92+
LOGGER.info("Load Post entities");
93+
94+
List<Post> posts = doInJPA(entityManager -> {
95+
return entityManager.createQuery("""
96+
select p
97+
from Post p
98+
""", Post.class)
99+
.setMaxResults(POST_COUNT)
100+
.getResultList();
101+
});
102+
103+
posts.forEach(post ->
104+
post.setTitle(post.getTitle().replaceAll("no", "nr"))
105+
);
106+
107+
LOGGER.info("Update Post entities");
108+
109+
doInStatelessSession(session -> {
110+
session.updateMultiple(posts);
111+
});
112+
}
113+
114+
@Test
115+
public void testDeleteMultiplePosts() {
116+
insertPosts();
117+
118+
LOGGER.info("testDeletePosts");
119+
doInStatelessSession(session -> {
120+
List<Post> posts = session.createQuery("""
121+
select p
122+
from Post p
123+
""", Post.class)
124+
.getResultList();
125+
126+
session.deleteMultiple(posts);
127+
});
128+
}
129+
130+
@Test
131+
public void testDeletePostsAndComments() {
132+
LOGGER.info("testDeletePostsAndComments");
133+
insertPostsAndComments();
134+
135+
List<Post> posts = doInJPA(entityManager -> {
136+
return entityManager.createQuery("""
137+
select p
138+
from Post p
139+
join fetch p.comments
140+
""", Post.class)
141+
.setMaxResults(POST_COUNT)
142+
.getResultList();
143+
});
144+
145+
doInStatelessSession(session -> {
146+
session.deleteMultiple(
147+
posts.stream()
148+
.flatMap(post -> post.getComments().stream())
149+
.toList()
150+
);
151+
session.deleteMultiple(posts);
152+
});
153+
}
154+
155+
private void insertPosts() {
156+
doInStatelessSession(session -> {
157+
for (long i = 1; i <= POST_COUNT; i++) {
158+
session.insert(
159+
new Post()
160+
.setId(i)
161+
.setTitle(String.format("Post no. %d", i))
162+
);
163+
}
164+
});
165+
}
166+
167+
private void insertPostsAndComments() {
168+
doInStatelessSession(session -> {
169+
List<Post> posts = LongStream.rangeClosed(1, POST_COUNT).boxed()
170+
.map(i -> {
171+
Post post = new Post()
172+
.setId(i)
173+
.setTitle(String.format("Post no. %d", i));
174+
175+
session.insert(post);
176+
177+
return post;
178+
})
179+
.toList();
180+
181+
final int COMMENT_COUNT = 5;
182+
183+
posts.forEach(post -> {
184+
for (long i = 1; i <= COMMENT_COUNT; i++) {
185+
session.insert(
186+
new PostComment()
187+
.setPost(post)
188+
.setReview(
189+
String.format(
190+
"Post comment no. %d",
191+
(post.getId() - 1) * COMMENT_COUNT + i
192+
)
193+
)
194+
);
195+
}
196+
});
197+
});
198+
}
199+
200+
@Entity(name = "Post")
201+
@Table(name = "post")
202+
public static class Post {
203+
204+
@Id
205+
private Long id;
206+
207+
private String title;
208+
209+
@OneToMany(cascade = CascadeType.ALL, mappedBy = "post",
210+
orphanRemoval = true)
211+
private List<PostComment> comments = new ArrayList<>();
212+
213+
public Long getId() {
214+
return id;
215+
}
216+
217+
public Post setId(Long id) {
218+
this.id = id;
219+
return this;
220+
}
221+
222+
public String getTitle() {
223+
return title;
224+
}
225+
226+
public Post setTitle(String title) {
227+
this.title = title;
228+
return this;
229+
}
230+
231+
public List<PostComment> getComments() {
232+
return comments;
233+
}
234+
235+
public Post addComment(PostComment comment) {
236+
comments.add(comment);
237+
comment.setPost(this);
238+
return this;
239+
}
240+
}
241+
242+
@Entity(name = "PostComment")
243+
@Table(name = "post_comment")
244+
public static class PostComment {
245+
246+
@Id
247+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
248+
@SequenceOptimizer(
249+
sequenceName = "seq_post_comment",
250+
optimizer = "pooled-lo"
251+
)
252+
private Long id;
253+
254+
@ManyToOne
255+
private Post post;
256+
257+
private String review;
258+
259+
public PostComment() {}
260+
261+
public PostComment(String review) {
262+
this.review = review;
263+
}
264+
265+
public Long getId() {
266+
return id;
267+
}
268+
269+
public void setId(Long id) {
270+
this.id = id;
271+
}
272+
273+
public Post getPost() {
274+
return post;
275+
}
276+
277+
public PostComment setPost(Post post) {
278+
this.post = post;
279+
return this;
280+
}
281+
282+
public String getReview() {
283+
return review;
284+
}
285+
286+
public PostComment setReview(String review) {
287+
this.review = review;
288+
return this;
289+
}
290+
}
291+
}

0 commit comments

Comments
 (0)