Skip to content

Commit

Permalink
Use byte arrays when cloning JSON objects #198
Browse files Browse the repository at this point in the history
Add tests to check the number of statements generated when persisting and fetching JSON Types
  • Loading branch information
vladmihalcea committed Mar 20, 2020
1 parent 6dae8ac commit 1b0fb16
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import com.vladmihalcea.hibernate.type.model.Ticket;
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest;
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.junit.Test;

import javax.persistence.*;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -44,11 +47,11 @@ protected void additionalProperties(Properties properties) {
properties.setProperty("hibernate.cache.region.factory_class", "ehcache");
}

@Test
public void test() {
final AtomicReference<Event> eventHolder = new AtomicReference<>();
private Event _event;

doInJPA((JPATransactionFunction<Void>) entityManager -> {
@Override
protected void afterInit() {
doInJPA(entityManager -> {
Event nullEvent = new Event();
nullEvent.setId(0L);
entityManager.persist(nullEvent);
Expand All @@ -63,11 +66,11 @@ public void test() {
event.setProperties(
JacksonUtil.toJsonNode(
"{" +
" \"title\": \"High-Performance Java Persistence\"," +
" \"author\": \"Vlad Mihalcea\"," +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99" +
"}"
" \"title\": \"High-Performance Java Persistence\"," +
" \"author\": \"Vlad Mihalcea\"," +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99" +
"}"
)
);
entityManager.persist(event);
Expand All @@ -76,14 +79,21 @@ public void test() {
ticket.setPrice(12.34d);
ticket.setRegistrationCode("ABC123");

eventHolder.set(event);

return null;
_event = event;
});
doInJPA((JPATransactionFunction<Void>) entityManager -> {
Event event = entityManager.find(Event.class, eventHolder.get().getId());
}

@Test
public void test() {
doInJPA(entityManager -> {
QueryCountHolder.clear();

Event event = entityManager.find(Event.class, _event.getId());
assertNotNull(event.getProperties());

QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(0, queryCount.getTotal());

List<String> properties = entityManager.createNativeQuery(
"select CAST(e.properties AS CHAR(1000)) " +
"from event e " +
Expand All @@ -93,8 +103,6 @@ public void test() {
assertEquals(1, properties.size());
JsonNode jsonNode = JacksonUtil.toJsonNode(properties.get(0));
assertEquals("High-Performance Java Persistence", jsonNode.get("title").asText());

return null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.vladmihalcea.hibernate.type.model.Location;
import com.vladmihalcea.hibernate.type.model.Ticket;
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.annotations.Type;
import org.junit.Test;

Expand Down Expand Up @@ -37,10 +39,10 @@ protected String[] packages() {
};
}

@Test
public void test() {
final AtomicReference<Event> eventHolder = new AtomicReference<>();
private Event _event;

@Override
protected void afterInit() {
doInJPA(entityManager -> {
Location location = new Location();
location.setCountry("Romania");
Expand All @@ -51,14 +53,25 @@ public void test() {
event.setAlternativeLocations(Arrays.asList(location));
entityManager.persist(event);

eventHolder.set(event);
_event = event;
});
}

@Test
public void test() {
QueryCountHolder.clear();

doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, eventHolder.get().getId());
Event event = entityManager.find(Event.class, _event.getId());
assertEquals(1, event.getAlternativeLocations().size());
assertEquals("Cluj-Napoca", event.getAlternativeLocations().get(0).getCity());
assertEquals("Romania", event.getAlternativeLocations().get(0).getCountry());
});

QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getSelect());
assertEquals(0, queryCount.getUpdate());
}

@Entity(name = "Event")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.vladmihalcea.hibernate.type.model.BaseEntity;
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.junit.Test;
Expand Down Expand Up @@ -32,10 +34,10 @@ protected Class<?>[] entities() {
};
}

@Test
public void test() {
final AtomicReference<User> userHolder = new AtomicReference<>();
private User _user;

@Override
protected void afterInit() {
doInJPA(entityManager -> {
User user = new User();

Expand All @@ -48,22 +50,17 @@ public void test() {
)));

entityManager.persist(user);
userHolder.set(user);
_user = user;
});
}

@Test
public void test() {
doInJPA(entityManager -> {
User user = entityManager.find(User.class, userHolder.get().getId());
assertEquals(new HashSet<>(asList("1234567", "7654321")), user.getPhones());
assertEquals(EnumSet.of(Role.USER, Role.ADMIN), user.getRoles());
assertEquals(new HashSet<>(asList(
new Child("John", 1, new HashSet<>(asList("toy4", "toy3"))),
new Child("Jane", 2, new HashSet<>(asList("toy2", "toy1")))
)), user.getChildren());
assertEquals(Integer.valueOf(0), user.getVersion());
});
User user = entityManager.find(User.class, _user.getId());

QueryCountHolder.clear();

doInJPA(entityManager -> {
User user = entityManager.find(User.class, userHolder.get().getId());
user.setPhones(new HashSet<>(asList("1592637", "9518473")));
user.setRoles(EnumSet.of(Role.USER, Role.DEV));
user.setChildren(new HashSet<>(asList(
Expand All @@ -72,8 +69,12 @@ public void test() {
)));
});

QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getUpdate());

doInJPA(entityManager -> {
User user = entityManager.find(User.class, userHolder.get().getId());
User user = entityManager.find(User.class, _user.getId());
assertEquals(new HashSet<>(asList("9518473", "1592637")), user.getPhones());
assertEquals(EnumSet.of(Role.DEV, Role.USER), user.getRoles());
assertEquals(new HashSet<>(asList(
Expand All @@ -84,6 +85,27 @@ public void test() {
});
}

@Test
public void testLoad() {
QueryCountHolder.clear();

doInJPA(entityManager -> {
User user = entityManager.find(User.class, _user.getId());
assertEquals(new HashSet<>(asList("1234567", "7654321")), user.getPhones());
assertEquals(EnumSet.of(Role.USER, Role.ADMIN), user.getRoles());
assertEquals(new HashSet<>(asList(
new Child("John", 1, new HashSet<>(asList("toy4", "toy3"))),
new Child("Jane", 2, new HashSet<>(asList("toy2", "toy1")))
)), user.getChildren());
assertEquals(Integer.valueOf(0), user.getVersion());
});

QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getSelect());
assertEquals(0, queryCount.getUpdate());
}

@Entity
@Table(name = "users")
@DynamicUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.vladmihalcea.hibernate.type.model.Location;
import com.vladmihalcea.hibernate.type.model.Ticket;
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.annotations.Type;
import org.junit.Test;

Expand All @@ -24,22 +26,24 @@ public class MySQLJsonTypeTest extends AbstractMySQLIntegrationTest {
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Participant.class
Event.class,
Participant.class
};
}

@Override
protected String[] packages() {
return new String[]{
Location.class.getPackage().getName()
Location.class.getPackage().getName()
};
}

@Test
public void test() {
final AtomicReference<Event> eventHolder = new AtomicReference<>();
final AtomicReference<Participant> participantHolder = new AtomicReference<>();
private Event _event;

private Participant _participant;

@Override
protected void afterInit() {

doInJPA(entityManager -> {
Event nullEvent = new Event();
Expand All @@ -66,21 +70,42 @@ public void test() {

entityManager.persist(participant);

eventHolder.set(event);
participantHolder.set(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, eventHolder.get().getId());
Event event = entityManager.find(Event.class, _event.getId());
assertEquals("Cluj-Napoca", event.getLocation().getCity());

Participant participant = entityManager.find(Participant.class, participantHolder.get().getId());
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();
"select p.ticket -> \"$.registrationCode\" " +
"from participant p " +
"where JSON_EXTRACT(p.ticket, \"$.price\") > 1 ")
.getResultList();

event.getLocation().setCity("Constanța");
entityManager.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class MySQLParametrizedJsonTypeTest extends AbstractMySQLIntegrationTest
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
PropertyHolder.class,
PropertyHolder.class,
};
}

@Override
protected String[] packages() {
return new String[]{
PropertyHolder.class.getPackage().getName()
PropertyHolder.class.getPackage().getName()
};
}

Expand Down Expand Up @@ -58,6 +58,7 @@ public void test() {

eventHolder.set(propertyHolder);
});

doInJPA(entityManager -> {
PropertyHolder propertyHolder = entityManager.find(PropertyHolder.class, eventHolder.get().getId());
assertEquals("value", propertyHolder.getProperty("key1"));
Expand Down
Loading

0 comments on commit 1b0fb16

Please sign in to comment.