From bbdfbc2f88d5fecf968551a68912860a11526bc1 Mon Sep 17 00:00:00 2001 From: YinAqu Date: Tue, 14 Mar 2023 21:35:15 +0800 Subject: [PATCH 1/2] Jackson's include-non-empty affects jsonb dirty check test --- ...InclusionNonEmptyObjectMapperSupplier.java | 12 ++ ...eJacksonPropertyInclusionNonEmptyTest.java | 168 ++++++++++++++++++ ...onPropertyInclusionNonEmptyTest.properties | 1 + 3 files changed, 181 insertions(+) create mode 100644 hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/JacksonPropertyInclusionNonEmptyObjectMapperSupplier.java create mode 100644 hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java create mode 100644 hypersistence-utils-hibernate-55/src/test/resources/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/JacksonPropertyInclusionNonEmptyObjectMapperSupplier.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/JacksonPropertyInclusionNonEmptyObjectMapperSupplier.java new file mode 100644 index 000000000..4483384ef --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/JacksonPropertyInclusionNonEmptyObjectMapperSupplier.java @@ -0,0 +1,12 @@ +package io.hypersistence.utils.hibernate.type.json; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.hypersistence.utils.hibernate.type.util.ObjectMapperSupplier; + +public class JacksonPropertyInclusionNonEmptyObjectMapperSupplier implements ObjectMapperSupplier { + @Override + public ObjectMapper get() { + return new ObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY); + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java new file mode 100644 index 000000000..572af0876 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java @@ -0,0 +1,168 @@ +package io.hypersistence.utils.hibernate.type.json; + +import io.hypersistence.utils.hibernate.type.util.Configuration; +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.junit.Test; + +import javax.persistence.*; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Random; + + +public class PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + MyEntity.class, + }; + } + + @Override + public void init() { + System.setProperty( + Configuration.PROPERTIES_FILE_PATH, + "PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties" + ); + super.init(); + } + + private MyEntity _myEntity; + + @Override + protected void afterInit() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + Inside inside = new Inside(); + inside.setAttribute("JPA"); + MyEntity entity = new MyEntity(); + entity.addInside(inside); + + entityManager.persist(entity); + + _myEntity = entity; + }); + + SQLStatementCountValidator.assertTotalCount(1); + SQLStatementCountValidator.assertInsertCount(1); + } + + @Test + public void testLoad() { + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + MyEntity myEntity = entityManager.find(MyEntity.class, _myEntity.getId()); + myEntity.clearInside(); + entityManager.createQuery("select id from MyEntity where flag=false").getSingleResult(); + }); + + SQLStatementCountValidator.assertTotalCount(3); + SQLStatementCountValidator.assertSelectCount(2); + SQLStatementCountValidator.assertUpdateCount(1); + } + + @TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)}) + @Entity(name = "MyEntity") + @Table(name = "my_entity") + public static class MyEntity { + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + private Long id; + + @Type(type = "jsonb") + @Column(columnDefinition = "jsonb") + private Outside outside; + + @Column + private boolean flag; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Outside getOutside() { + return outside; + } + + public void addInside(Inside inside) { + if (outside == null) { + outside = new Outside(); + } + outside.addInside(inside); + } + + public void clearInside() { + outside.clearInside(); + } + } + + public static class Outside { + private Map insides; + + public Map getInsides() { + return insides; + } + + public void addInside(Inside inside) { + if (insides == null) { + insides = new LinkedHashMap<>(); + } + insides.put(new Random().nextLong(), inside); + } + + public void clearInside() { + if (insides != null) { + insides.clear(); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Outside outside = (Outside) o; + return Objects.equals(insides, outside.insides); + } + + @Override + public int hashCode() { + return Objects.hash(insides); + } + } + + public static class Inside { + private String attribute; + + public String getAttribute() { + return attribute; + } + + public void setAttribute(String attribute) { + this.attribute = attribute; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Inside inside = (Inside) o; + return Objects.equals(attribute, inside.attribute); + } + + @Override + public int hashCode() { + return Objects.hash(attribute); + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/resources/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties b/hypersistence-utils-hibernate-55/src/test/resources/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties new file mode 100644 index 000000000..b21037af1 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/resources/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.properties @@ -0,0 +1 @@ +hypersistence.utils.jackson.object.mapper=io.hypersistence.utils.hibernate.type.json.JacksonPropertyInclusionNonEmptyObjectMapperSupplier From 2e72e64c35c94b7259fe9321eb30a6919d2fd81a Mon Sep 17 00:00:00 2001 From: YinAqu Date: Wed, 15 Mar 2023 11:16:41 +0800 Subject: [PATCH 2/2] Simplify test data structures --- ...eJacksonPropertyInclusionNonEmptyTest.java | 106 ++++++++---------- 1 file changed, 46 insertions(+), 60 deletions(-) diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java index 572af0876..b73a46f90 100644 --- a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/json/PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest.java @@ -9,17 +9,16 @@ import org.junit.Test; import javax.persistence.*; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; -import java.util.Random; public class PostgreSQLJsonBinaryTypeJacksonPropertyInclusionNonEmptyTest extends AbstractPostgreSQLIntegrationTest { @Override protected Class[] entities() { return new Class[]{ - MyEntity.class, + MyEntity.class, }; } @@ -39,11 +38,8 @@ protected void afterInit() { SQLStatementCountValidator.reset(); doInJPA(entityManager -> { - Inside inside = new Inside(); - inside.setAttribute("JPA"); MyEntity entity = new MyEntity(); - entity.addInside(inside); - + entity.addAttr("JPA"); entityManager.persist(entity); _myEntity = entity; @@ -59,7 +55,19 @@ public void testLoad() { doInJPA(entityManager -> { MyEntity myEntity = entityManager.find(MyEntity.class, _myEntity.getId()); - myEntity.clearInside(); + myEntity.addAttr("Hibernate"); + entityManager.createQuery("select id from MyEntity where flag=false").getSingleResult(); + }); + + SQLStatementCountValidator.assertTotalCount(3); + SQLStatementCountValidator.assertSelectCount(2); + SQLStatementCountValidator.assertUpdateCount(1); + + SQLStatementCountValidator.reset(); + + doInJPA(entityManager -> { + MyEntity myEntity = entityManager.find(MyEntity.class, _myEntity.getId()); + myEntity.clearAttr(); entityManager.createQuery("select id from MyEntity where flag=false").getSingleResult(); }); @@ -76,13 +84,13 @@ public static class MyEntity { @Id private Long id; - @Type(type = "jsonb") - @Column(columnDefinition = "jsonb") - private Outside outside; - @Column private boolean flag; + @Type(type = "jsonb") + @Column(columnDefinition = "jsonb") + private Post post; + public Long getId() { return id; } @@ -91,78 +99,56 @@ public void setId(Long id) { this.id = id; } - public Outside getOutside() { - return outside; + public Post getPost() { + return post; } - public void addInside(Inside inside) { - if (outside == null) { - outside = new Outside(); + public void addAttr(String attr) { + if (post == null) { + post = new Post(); } - outside.addInside(inside); + post.addAttr(attr); } - public void clearInside() { - outside.clearInside(); + public void clearAttr() { + if (post != null) { + post.clearAttr(); + } } } - public static class Outside { - private Map insides; - - public Map getInsides() { - return insides; - } + public static class Post { + private List attributes; - public void addInside(Inside inside) { - if (insides == null) { - insides = new LinkedHashMap<>(); + public void addAttr(String attr) { + if (attributes == null) { + attributes = new ArrayList<>(); } - insides.put(new Random().nextLong(), inside); + attributes.add(attr); } - public void clearInside() { - if (insides != null) { - insides.clear(); + public void clearAttr() { + if (attributes != null) { + attributes.clear(); } } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Outside outside = (Outside) o; - return Objects.equals(insides, outside.insides); - } - - @Override - public int hashCode() { - return Objects.hash(insides); - } - } - - public static class Inside { - private String attribute; - - public String getAttribute() { - return attribute; - } - - public void setAttribute(String attribute) { - this.attribute = attribute; + public List getAttributes() { + return attributes; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Inside inside = (Inside) o; - return Objects.equals(attribute, inside.attribute); + Post outside = (Post) o; + return Objects.equals(attributes, outside.attributes); } @Override public int hashCode() { - return Objects.hash(attribute); + return Objects.hash(attributes); } } + }