-
Notifications
You must be signed in to change notification settings - Fork 361
/
MySQLJsonTypeTest.java
160 lines (124 loc) · 4.39 KB
/
MySQLJsonTypeTest.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
package com.vladmihalcea.hibernate.type.json;
import com.vladmihalcea.hibernate.type.model.BaseEntity;
import com.vladmihalcea.hibernate.type.model.Location;
import com.vladmihalcea.hibernate.type.model.Ticket;
import com.vladmihalcea.hibernate.util.AbstractMySQLIntegrationTest;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.annotations.Type;
import org.junit.Test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class MySQLJsonTypeTest extends AbstractMySQLIntegrationTest {
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Participant.class
};
}
@Override
protected String[] packages() {
return new String[]{
Location.class.getPackage().getName()
};
}
private Event _event;
private Participant _participant;
@Override
protected void afterInit() {
doInJPA(entityManager -> {
Event nullEvent = new Event();
nullEvent.setId(0L);
entityManager.persist(nullEvent);
Location location = new Location();
location.setCountry("Romania");
location.setCity("Cluj-Napoca");
Event event = new Event();
event.setId(1L);
event.setLocation(location);
entityManager.persist(event);
Ticket ticket = new Ticket();
ticket.setPrice(12.34d);
ticket.setRegistrationCode("ABC123");
Participant participant = new Participant();
participant.setId(1L);
participant.setTicket(ticket);
participant.setEvent(event);
entityManager.persist(participant);
_event = event;
_participant = participant;
});
}
@Test
public void testLoad() {
QueryCountHolder.clear();
doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, _event.getId());
assertEquals("Romania", event.getLocation().getCountry());
assertEquals("Cluj-Napoca", event.getLocation().getCity());
});
QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getSelect());
assertEquals(0, queryCount.getUpdate());
}
@Test
public void test() {
doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, _event.getId());
assertEquals("Cluj-Napoca", event.getLocation().getCity());
Participant participant = entityManager.find(Participant.class, _participant.getId());
assertEquals("ABC123", participant.getTicket().getRegistrationCode());
List<String> participants = entityManager.createNativeQuery(
"select p.ticket -> \"$.registrationCode\" " +
"from participant p " +
"where JSON_EXTRACT(p.ticket, \"$.price\") > 1 ")
.getResultList();
event.getLocation().setCity("Constanța");
entityManager.flush();
assertEquals(1, participants.size());
});
}
@Entity(name = "Event")
@Table(name = "event")
public static class Event extends BaseEntity {
@Type(type = "com.vladmihalcea.hibernate.type.json.JsonType")
@Column(columnDefinition = "json")
private Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
@Entity(name = "Participant")
@Table(name = "participant")
public static class Participant extends BaseEntity {
@Type(type = "json")
@Column(columnDefinition = "json")
private Ticket ticket;
@ManyToOne
private Event event;
public Ticket getTicket() {
return ticket;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
}
}