/
FlushModeTest.java
81 lines (66 loc) · 2.95 KB
/
FlushModeTest.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
package kangwoojin.github.io.querydsl;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import com.querydsl.jpa.impl.JPAQuery;
import kangwoojin.github.io.querydsl.event.model.Campaign;
import kangwoojin.github.io.querydsl.event.model.Event;
import kangwoojin.github.io.querydsl.event.model.QCampaign;
import kangwoojin.github.io.querydsl.event.model.QEvent;
@DataJpaTest
class FlushModeTest {
QCampaign qCampaign = QCampaign.campaign;
QEvent qEvent = QEvent.event;
EntityManager entityManager;
@Autowired
private TestEntityManager testEntityManager;
@BeforeEach
void setUp() {
entityManager = testEntityManager.getEntityManager();
}
private Campaign generateCampaign(String name, Long amount) {
Campaign campaign = new Campaign();
campaign.setName(name);
campaign.setAmount(amount);
Event event = new Event();
event.setAmount(5L);
campaign.setEvents(List.of(event));
Campaign savedCampaign = testEntityManager.persist(campaign);
event.setCampaign(campaign);
return savedCampaign;
}
@Test
void flushModeWhenFlushModeIsAutoTest() {
String name = RandomStringUtils.randomAlphabetic(5);
long amount = RandomUtils.nextLong();
Campaign campaign = generateCampaign(name, amount);
JPAQuery<Campaign> query = new JPAQuery(entityManager);
List<Campaign> campaigns = query.select(qCampaign)
.from(qCampaign)
.join(qCampaign.events)
.setFlushMode(FlushModeType.AUTO) // default
.fetch(); // fetch시에 flush mode에 따라서 flush가 발생
assertThat(campaigns).hasSize(1);
}
@Test
void flushModeWhenFlushModeIsCommitTest() {
String name = RandomStringUtils.randomAlphabetic(5);
long amount = RandomUtils.nextLong();
Campaign campaign = generateCampaign(name, amount);
JPAQuery<Campaign> query = new JPAQuery(entityManager);
List<Campaign> campaigns = query.select(qCampaign)
.from(qCampaign)
.join(qCampaign.events)
.setFlushMode(FlushModeType.COMMIT)
.fetch(); // fetch시에 flush mode에 따라서 flush가 발생
assertThat(campaigns).hasSize(0);
}
}