Skip to content

Commit ecf89dc

Browse files
committed
Add bulk update tests
1 parent 92eddb1 commit ecf89dc

File tree

7 files changed

+742
-1
lines changed

7 files changed

+742
-1
lines changed

hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.hypersistence.utils.hibernate.type.json.generic;
22

3+
import io.hypersistence.utils.hibernate.type.json.JsonType;
34
import io.hypersistence.utils.hibernate.type.model.BaseEntity;
45
import io.hypersistence.utils.hibernate.type.model.Location;
56
import io.hypersistence.utils.hibernate.type.model.Ticket;
67
import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest;
78
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
89
import org.hibernate.annotations.Type;
10+
import org.hibernate.query.NativeQuery;
911
import org.junit.Test;
1012

1113
import javax.persistence.Column;
@@ -71,6 +73,27 @@ public void test() {
7173
SQLStatementCountValidator.assertUpdateCount(0);
7274
}
7375

76+
@Test
77+
public void testBulkUpdate() {
78+
doInJPA(entityManager -> {
79+
Location location = new Location();
80+
location.setCountry("Romania");
81+
location.setCity("Sibiu");
82+
83+
entityManager.createNativeQuery(
84+
"update Event " +
85+
"set location = :location " +
86+
"where id = :id")
87+
.setParameter("id", _event.getId())
88+
.unwrap(NativeQuery.class)
89+
.setParameter("location", location, new JsonType(Location.class))
90+
.executeUpdate();
91+
92+
Event event = entityManager.find(Event.class, _event.getId());
93+
assertEquals("Sibiu", event.getLocation().getCity());
94+
});
95+
}
96+
7497
@Entity(name = "Event")
7598
@Table(name = "event")
7699
public static class Event extends BaseEntity {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package io.hypersistence.utils.hibernate.type.json.generic;
2+
3+
import io.hypersistence.utils.hibernate.type.json.JsonType;
4+
import io.hypersistence.utils.hibernate.type.model.BaseEntity;
5+
import io.hypersistence.utils.hibernate.type.model.Location;
6+
import io.hypersistence.utils.hibernate.type.model.Ticket;
7+
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
8+
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
9+
import org.hibernate.annotations.Type;
10+
import org.hibernate.query.NativeQuery;
11+
import org.junit.Test;
12+
13+
import javax.persistence.Column;
14+
import javax.persistence.Entity;
15+
import javax.persistence.ManyToOne;
16+
import javax.persistence.Table;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
/**
21+
* @author Vlad Mihalcea
22+
*/
23+
public class GenericPostgreSQLJsonTypeTest extends AbstractPostgreSQLIntegrationTest {
24+
25+
@Override
26+
protected Class<?>[] entities() {
27+
return new Class<?>[]{
28+
Event.class,
29+
Participant.class
30+
};
31+
}
32+
33+
@Override
34+
protected String[] packages() {
35+
return new String[]{
36+
Location.class.getPackage().getName()
37+
};
38+
}
39+
40+
private Event _event;
41+
42+
private Participant _participant;
43+
44+
@Override
45+
protected void afterInit() {
46+
47+
doInJPA(entityManager -> {
48+
Event nullEvent = new Event();
49+
nullEvent.setId(0L);
50+
entityManager.persist(nullEvent);
51+
52+
Location location = new Location();
53+
location.setCountry("Romania");
54+
location.setCity("Cluj-Napoca");
55+
56+
Event event = new Event();
57+
event.setId(1L);
58+
event.setLocation(location);
59+
entityManager.persist(event);
60+
61+
Ticket ticket = new Ticket();
62+
ticket.setPrice(12.34d);
63+
ticket.setRegistrationCode("ABC123");
64+
65+
Participant participant = new Participant();
66+
participant.setId(1L);
67+
participant.setTicket(ticket);
68+
participant.setEvent(event);
69+
70+
entityManager.persist(participant);
71+
72+
_event = event;
73+
_participant = participant;
74+
});
75+
}
76+
77+
@Test
78+
public void testLoad() {
79+
SQLStatementCountValidator.reset();
80+
81+
doInJPA(entityManager -> {
82+
Event event = entityManager.find(Event.class, _event.getId());
83+
assertEquals("Romania", event.getLocation().getCountry());
84+
assertEquals("Cluj-Napoca", event.getLocation().getCity());
85+
});
86+
87+
SQLStatementCountValidator.assertTotalCount(1);
88+
SQLStatementCountValidator.assertSelectCount(1);
89+
SQLStatementCountValidator.assertUpdateCount(0);
90+
}
91+
92+
@Test
93+
public void testBulkUpdate() {
94+
doInJPA(entityManager -> {
95+
Location location = new Location();
96+
location.setCountry("Romania");
97+
location.setCity("Sibiu");
98+
99+
entityManager.createNativeQuery(
100+
"update Event " +
101+
"set location = :location " +
102+
"where id = :id")
103+
.setParameter("id", _event.getId())
104+
.unwrap(NativeQuery.class)
105+
.setParameter("location", location, new JsonType(Location.class))
106+
.executeUpdate();
107+
108+
Event event = entityManager.find(Event.class, _event.getId());
109+
assertEquals("Sibiu", event.getLocation().getCity());
110+
});
111+
}
112+
113+
@Entity(name = "Event")
114+
@Table(name = "event")
115+
public static class Event extends BaseEntity {
116+
117+
@Type(type = "io.hypersistence.utils.hibernate.type.json.JsonType")
118+
@Column(columnDefinition = "jsonb")
119+
private Location location;
120+
121+
public Location getLocation() {
122+
return location;
123+
}
124+
125+
public void setLocation(Location location) {
126+
this.location = location;
127+
}
128+
}
129+
130+
@Entity(name = "Participant")
131+
@Table(name = "participant")
132+
public static class Participant extends BaseEntity {
133+
134+
@Type(type = "io.hypersistence.utils.hibernate.type.json.JsonType")
135+
@Column(columnDefinition = "jsonb")
136+
private Ticket ticket;
137+
138+
@ManyToOne
139+
private Event event;
140+
141+
public Ticket getTicket() {
142+
return ticket;
143+
}
144+
145+
public void setTicket(Ticket ticket) {
146+
this.ticket = ticket;
147+
}
148+
149+
public Event getEvent() {
150+
return event;
151+
}
152+
153+
public void setEvent(Event event) {
154+
this.event = event;
155+
}
156+
}
157+
}

hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.hypersistence.utils.hibernate.type.json.generic;
22

3+
import io.hypersistence.utils.hibernate.type.json.JsonType;
34
import io.hypersistence.utils.hibernate.type.model.BaseEntity;
45
import io.hypersistence.utils.hibernate.type.model.Location;
56
import io.hypersistence.utils.hibernate.type.model.Ticket;
67
import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest;
78
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
89
import org.hibernate.annotations.Type;
10+
import org.hibernate.query.NativeQuery;
911
import org.junit.Test;
1012

1113
import javax.persistence.Column;
@@ -111,6 +113,27 @@ public void test() {
111113
});
112114
}
113115

116+
@Test
117+
public void testBulkUpdate() {
118+
doInJPA(entityManager -> {
119+
Location location = new Location();
120+
location.setCountry("Romania");
121+
location.setCity("Sibiu");
122+
123+
entityManager.createNativeQuery(
124+
"update Event " +
125+
"set location = :location " +
126+
"where id = :id")
127+
.setParameter("id", _event.getId())
128+
.unwrap(NativeQuery.class)
129+
.setParameter("location", location, new JsonType(Location.class))
130+
.executeUpdate();
131+
132+
Event event = entityManager.find(Event.class, _event.getId());
133+
assertEquals("Sibiu", event.getLocation().getCity());
134+
});
135+
}
136+
114137
@Entity(name = "Event")
115138
@Table(name = "event")
116139
public static class Event extends BaseEntity {

0 commit comments

Comments
 (0)