From a2224e9b5f117b82346a43a257a352db4788aed9 Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Sat, 31 Dec 2022 08:43:47 +0200 Subject: [PATCH] Add bulk update tests --- .../generic/GenericMySQLJsonTypeTest.java | 23 +++ .../GenericPostgreSQLJsonTypeTest.java | 157 +++++++++++++++ .../generic/GenericMySQLJsonTypeTest.java | 23 +++ .../GenericPostgreSQLJsonTypeTest.java | 159 ++++++++++++++++ .../generic/GenericMySQLJsonTypeTest.java | 44 ++++- .../GenericMySQLRegisterJsonTypeTest.java | 179 ++++++++++++++++++ .../GenericPostgreSQLJsonTypeTest.java | 159 ++++++++++++++++ 7 files changed, 743 insertions(+), 1 deletion(-) create mode 100644 hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java create mode 100644 hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java create mode 100644 hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLRegisterJsonTypeTest.java create mode 100644 hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java index 30ff921c0..e5d6b92c3 100644 --- a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java @@ -1,11 +1,13 @@ package io.hypersistence.utils.hibernate.type.json.generic; +import io.hypersistence.utils.hibernate.type.json.JsonType; import io.hypersistence.utils.hibernate.type.model.BaseEntity; import io.hypersistence.utils.hibernate.type.model.Location; import io.hypersistence.utils.hibernate.type.model.Ticket; import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest; import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; import org.junit.Test; import javax.persistence.Column; @@ -71,6 +73,27 @@ public void test() { SQLStatementCountValidator.assertUpdateCount(0); } + @Test + public void testBulkUpdate() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + @Entity(name = "Event") @Table(name = "event") public static class Event extends BaseEntity { diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java new file mode 100644 index 000000000..2df0690dd --- /dev/null +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java @@ -0,0 +1,157 @@ +package io.hypersistence.utils.hibernate.type.json.generic; + +import io.hypersistence.utils.hibernate.type.json.JsonType; +import io.hypersistence.utils.hibernate.type.model.BaseEntity; +import io.hypersistence.utils.hibernate.type.model.Location; +import io.hypersistence.utils.hibernate.type.model.Ticket; +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; +import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; +import org.junit.Test; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +public class GenericPostgreSQLJsonTypeTest extends AbstractPostgreSQLIntegrationTest { + + @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() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Romania", event.getLocation().getCountry()); + assertEquals("Cluj-Napoca", event.getLocation().getCity()); + }); + + SQLStatementCountValidator.assertTotalCount(1); + SQLStatementCountValidator.assertSelectCount(1); + SQLStatementCountValidator.assertUpdateCount(0); + } + + @Test + public void testBulkUpdate() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + + @Entity(name = "Event") + @Table(name = "event") + public static class Event extends BaseEntity { + + @Type(type = "io.hypersistence.utils.hibernate.type.json.JsonType") + @Column(columnDefinition = "jsonb") + 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 = "io.hypersistence.utils.hibernate.type.json.JsonType") + @Column(columnDefinition = "jsonb") + 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; + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java index aae4b8dcc..6b1ec037d 100644 --- a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java @@ -1,11 +1,13 @@ package io.hypersistence.utils.hibernate.type.json.generic; +import io.hypersistence.utils.hibernate.type.json.JsonType; import io.hypersistence.utils.hibernate.type.model.BaseEntity; import io.hypersistence.utils.hibernate.type.model.Location; import io.hypersistence.utils.hibernate.type.model.Ticket; import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest; import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; import org.junit.Test; import javax.persistence.Column; @@ -111,6 +113,27 @@ public void test() { }); } + @Test + public void testBulkUpdate() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + @Entity(name = "Event") @Table(name = "event") public static class Event extends BaseEntity { diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java new file mode 100644 index 000000000..ad88d5b44 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java @@ -0,0 +1,159 @@ +package io.hypersistence.utils.hibernate.type.json.generic; + +import io.hypersistence.utils.hibernate.type.json.JsonType; +import io.hypersistence.utils.hibernate.type.model.BaseEntity; +import io.hypersistence.utils.hibernate.type.model.Location; +import io.hypersistence.utils.hibernate.type.model.Ticket; +import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest; +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; +import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; +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 GenericPostgreSQLJsonTypeTest extends AbstractPostgreSQLIntegrationTest { + + @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() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Romania", event.getLocation().getCountry()); + assertEquals("Cluj-Napoca", event.getLocation().getCity()); + }); + + SQLStatementCountValidator.assertTotalCount(1); + SQLStatementCountValidator.assertSelectCount(1); + SQLStatementCountValidator.assertUpdateCount(0); + } + + @Test + public void testBulkUpdate() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + + @Entity(name = "Event") + @Table(name = "event") + public static class Event extends BaseEntity { + + @Type(type = "io.hypersistence.utils.hibernate.type.json.JsonType") + @Column(columnDefinition = "jsonb") + 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 = "io.hypersistence.utils.hibernate.type.json.JsonType") + @Column(columnDefinition = "jsonb") + 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; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java index 57736cc22..7c3336027 100644 --- a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLJsonTypeTest.java @@ -11,6 +11,9 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; +import org.hibernate.query.Query; +import org.hibernate.query.TypedParameterValue; import org.junit.Test; import java.util.List; @@ -91,7 +94,6 @@ public void testLoad() { @Test public void test() { - doInJPA(entityManager -> { Event event = entityManager.find(Event.class, _event.getId()); assertEquals("Cluj-Napoca", event.getLocation().getCity()); @@ -113,6 +115,46 @@ public void test() { }); } + @Test + public void testBulkUpdateNativeQuery() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", new TypedParameterValue<>(new JsonType(Location.class), location)) + .setParameter("location", location) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + @Entity(name = "Event") @Table(name = "event") public static class Event extends BaseEntity { diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLRegisterJsonTypeTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLRegisterJsonTypeTest.java new file mode 100644 index 000000000..fc538e76e --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericMySQLRegisterJsonTypeTest.java @@ -0,0 +1,179 @@ +package io.hypersistence.utils.hibernate.type.json.generic; + +import io.hypersistence.utils.hibernate.type.json.JsonBinaryType; +import io.hypersistence.utils.hibernate.type.json.JsonType; +import io.hypersistence.utils.hibernate.type.json.configuration.CustomObjectMapperSupplier; +import io.hypersistence.utils.hibernate.type.json.configuration.PostgreSQLJsonBinaryTypeProgrammaticConfigurationTest; +import io.hypersistence.utils.hibernate.type.model.BaseEntity; +import io.hypersistence.utils.hibernate.type.model.Location; +import io.hypersistence.utils.hibernate.type.model.Ticket; +import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest; +import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import org.hibernate.annotations.Type; +import org.hibernate.jpa.boot.spi.TypeContributorList; +import org.hibernate.query.NativeQuery; +import org.hibernate.query.Query; +import org.hibernate.query.TypedParameterValue; +import org.junit.Test; + +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +public class GenericMySQLRegisterJsonTypeTest extends AbstractMySQLIntegrationTest { + + @Override + protected Class[] entities() { + return new Class[]{ + Event.class, + Participant.class + }; + } + + @Override + protected String[] packages() { + return new String[]{ + Location.class.getPackage().getName() + }; + } + + @Override + protected void additionalProperties(Properties properties) { + JsonType locationJsonType = new JsonType(Location.class); + + properties.put("hibernate.type_contributors", + (TypeContributorList) () -> Collections.singletonList( + (typeContributions, serviceRegistry) -> + typeContributions.contributeType(locationJsonType) + ) + ); + } + + + 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() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Romania", event.getLocation().getCountry()); + assertEquals("Cluj-Napoca", event.getLocation().getCity()); + }); + + SQLStatementCountValidator.assertTotalCount(1); + SQLStatementCountValidator.assertSelectCount(1); + SQLStatementCountValidator.assertUpdateCount(0); + } + + @Test + public void testBulkUpdateJpqlQuery() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(Query.class) + .setParameter("location", location) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + + @Entity(name = "Event") + @Table(name = "event") + public static class Event extends BaseEntity { + + @Type(JsonType.class) + @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(JsonType.class) + @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; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java new file mode 100644 index 000000000..983196fef --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/json/generic/GenericPostgreSQLJsonTypeTest.java @@ -0,0 +1,159 @@ +package io.hypersistence.utils.hibernate.type.json.generic; + +import io.hypersistence.utils.hibernate.type.json.JsonType; +import io.hypersistence.utils.hibernate.type.model.BaseEntity; +import io.hypersistence.utils.hibernate.type.model.Location; +import io.hypersistence.utils.hibernate.type.model.Ticket; +import io.hypersistence.utils.hibernate.util.AbstractMySQLIntegrationTest; +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import org.hibernate.annotations.Type; +import org.hibernate.query.NativeQuery; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +public class GenericPostgreSQLJsonTypeTest extends AbstractPostgreSQLIntegrationTest { + + @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() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Romania", event.getLocation().getCountry()); + assertEquals("Cluj-Napoca", event.getLocation().getCity()); + }); + + SQLStatementCountValidator.assertTotalCount(1); + SQLStatementCountValidator.assertSelectCount(1); + SQLStatementCountValidator.assertUpdateCount(0); + } + + @Test + public void testBulkUpdate() { + doInJPA(entityManager -> { + Location location = new Location(); + location.setCountry("Romania"); + location.setCity("Sibiu"); + + entityManager.createNativeQuery( + "update Event " + + "set location = :location " + + "where id = :id") + .setParameter("id", _event.getId()) + .unwrap(NativeQuery.class) + .setParameter("location", location, new JsonType(Location.class)) + .executeUpdate(); + + Event event = entityManager.find(Event.class, _event.getId()); + assertEquals("Sibiu", event.getLocation().getCity()); + }); + } + + @Entity(name = "Event") + @Table(name = "event") + public static class Event extends BaseEntity { + + @Type(JsonType.class) + @Column(columnDefinition = "jsonb") + 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(JsonType.class) + @Column(columnDefinition = "jsonb") + 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; + } + } +}