-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToManyTest.java
225 lines (192 loc) · 8.93 KB
/
ToManyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package io.amplicode.jpa.repository;
import io.amplicode.jpa.model.Post;
import io.amplicode.jpa.model.Post_;
import io.amplicode.jpa.model.User_;
import io.amplicode.jpa.projection.PostWithLikeUsersDto;
import io.amplicode.jpa.projection.PostWithLikeUsersFlat;
import io.amplicode.jpa.projection.UserPresentationDto;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Tuple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import static io.amplicode.jpa.InitTestDataService.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
@ActiveProfiles("postgres")
public class ToManyTest {
@Autowired
private PostRepository postRepository;
@PersistenceContext
private EntityManager em;
/**
* Работает это как-то так. Сам hibernate не умеет нормально мапить коллекционные атрибуты в DTO/Projection.
* HQL вернет нам n + m, где n - это количество записей post по нашему запросу, а m это сумма всех связанных с этими постами like users.
* Условно, запрос будет такого вида
* <code>
* Hibernate:
* select
* p1_0.id,
* p1_0.slug,
* p1_0.title,
* lu1_0.user_id,
* lu1_1.username
* from
* posts p1_0
* left join
* post_like_users lu1_0
* on p1_0.id=lu1_0.post_id
* left join
* users lu1_1
* on lu1_1.id=lu1_0.user_id
* where
* upper(p1_0.title) like upper(?) escape '\'
* </code>
* <p>
* На наших тестовых данных есть всего один post, который удовлетворяет этому запросу, но записей вернеться две,
* т.к. у этого поста два like users, и произойдет некоторый дубляж данных для p1_0.id, p1_0.slug, p1_0.title
*/
@Test
void derivedMethodInterfaceFlatPrj() throws Exception {
List<PostWithLikeUsersFlat> posts = postRepository.findAllByTitleContainsIgnoreCase("spring", PostWithLikeUsersFlat.class);
assertEquals(2, posts.size());
var post = posts.getFirst();
assertEquals(POST1_SLUG, post.getSlug());
PostWithLikeUsersFlat post1 = posts.get(0);
PostWithLikeUsersFlat post2 = posts.get(1);
assertEquals(post1.getId(), post2.getId());
assertEquals(POST1_AUTHOR_NAME, post1.getLikeUsersUsername());
assertEquals(POST2_AUTHOR_NAME, post2.getLikeUsersUsername());
}
/**
* Такой же side effect как и в кейсе с проекцией, загружается n + m записей.
*/
@Test
void derivedMethodClassFlat() {
List<PostWithLikeUsersDto> post = postRepository.findAllByTitleContainsIgnoreCase("spring", PostWithLikeUsersDto.class);
assertEquals(2, post.size());
var postFirst = post.getFirst();
assertEquals(POST1_SLUG, postFirst.slug());
var post1 = post.get(0);
var post2 = post.get(1);
assertEquals(post1.id(), post2.id());
assertEquals(POST1_AUTHOR_NAME, post1.likeUsersUsername());
assertEquals(POST2_AUTHOR_NAME, post2.likeUsersUsername());
}
@Test
void queryMethodInterfaceFlatPrj() {
var posts = postRepository.findInterfaceFlatPrj("spring");
assertEquals(2, posts.size());
var post = posts.getFirst();
assertEquals(POST1_SLUG, post.getSlug());
var post1 = posts.get(0);
var post2 = posts.get(1);
assertEquals(post1.getId(), post2.getId());
assertEquals(POST1_AUTHOR_NAME, post1.getLikeUsersUsername());
assertEquals(POST2_AUTHOR_NAME, post2.getLikeUsersUsername());
}
@Test
void queryMethodClassFlatPrj() {
var posts = postRepository.findToManyClassFlat("spring");
assertEquals(2, posts.size());
var post = posts.getFirst();
assertEquals(POST1_SLUG, post.slug());
var post1 = posts.get(0);
var post2 = posts.get(1);
assertEquals(post1.id(), post2.id());
assertEquals(POST1_AUTHOR_NAME, post1.likeUsersUsername());
assertEquals(POST2_AUTHOR_NAME, post2.likeUsersUsername());
}
@Test
void queryMethodClassNested() {
var posts = postRepository.findToManyClassNested("spring");
assertEquals(2, posts.size());
var post = posts.getFirst();
assertEquals(POST1_SLUG, post.slug());
var post1 = posts.get(0);
var post2 = posts.get(1);
assertEquals(post1.id(), post2.id());
assertEquals(POST1_AUTHOR_NAME, post1.likeUser().username());
assertEquals(POST2_AUTHOR_NAME, post2.likeUser().username());
}
/**
* В данном примере, мы выполняем запрос через @Query метод и получаем Tuple как результат.
* Затем мы перекладываем выгруженные данные уже в готовый DTO класс.
* Также решаем проблему схлопывания дубликатов
*/
@Test
void queryMethodTuple() {
var postTuples = postRepository.findToManyTuple("spring");
assertEquals(2, postTuples.size());
var postMap = new LinkedHashMap<Long, PostWithLikeUsers>();
for (Tuple tuple : postTuples) {
var postId = tuple.get("id", Long.class);
var postWithLikeUsers = postMap.computeIfAbsent(postId, id ->
new PostWithLikeUsers(
id,
tuple.get("slug", String.class),
tuple.get("title", String.class),
new ArrayList<>()
));
postWithLikeUsers.likeUsers.add(new UserPresentationDto(
tuple.get("likeUserId", Long.class),
tuple.get("likeUserUsername", String.class)
));
}
List<PostWithLikeUsers> posts = postMap.values().stream().toList();
assertEquals(1, posts.size());
PostWithLikeUsers firstPost = posts.getFirst();
assertEquals(POST1_SLUG, firstPost.slug());
List<UserPresentationDto> likeUsers = firstPost.likeUsers;
assertEquals(2, likeUsers.size());
assertEquals(POST1_AUTHOR_NAME, likeUsers.get(0).username());
assertEquals(POST2_AUTHOR_NAME, likeUsers.get(1).username());
}
@Test
void criteriaTuple() {
var cb = em.getCriteriaBuilder();
var query = cb.createTupleQuery();
var owner = query.from(Post.class);
var idPath = owner.<Long>get(Post_.ID);
var slugPath = owner.<String>get(Post_.SLUG);
var titlePath = owner.<String>get(Post_.TITLE);
var likeUsersIdPath = owner.get(Post_.LIKE_USERS).<Long>get(User_.ID);
var likeUsersUsernamePath = owner.get(Post_.LIKE_USERS).<String>get(User_.USERNAME);
query.select(cb.tuple(idPath, slugPath, titlePath, likeUsersIdPath, likeUsersUsernamePath))
.where(cb.like(cb.lower(titlePath), "%spring%"));
List<Tuple> postTuples = em.createQuery(query).getResultList();
var postMap = new LinkedHashMap<Long, PostWithLikeUsers>();
for (Tuple tuple : postTuples) {
var postId = tuple.get(idPath);
var postWithLikeUsers = postMap.computeIfAbsent(postId, id ->
new PostWithLikeUsers(
id,
tuple.get(slugPath),
tuple.get(titlePath),
new ArrayList<>()
));
postWithLikeUsers.likeUsers.add(new UserPresentationDto(
tuple.get(likeUsersIdPath),
tuple.get(likeUsersUsernamePath)
));
}
List<PostWithLikeUsers> posts = postMap.values().stream().toList();
assertEquals(1, posts.size());
PostWithLikeUsers firstPost = posts.getFirst();
assertEquals(POST1_SLUG, firstPost.slug());
List<UserPresentationDto> likeUsers = firstPost.likeUsers;
assertEquals(2, likeUsers.size());
assertEquals(POST1_AUTHOR_NAME, likeUsers.get(0).username());
assertEquals(POST2_AUTHOR_NAME, likeUsers.get(1).username());
}
private record PostWithLikeUsers(Long id,
String slug,
String title,
List<UserPresentationDto> likeUsers) {
}
}