hypersistence-utils: Unexpected dirty checking behavior on collections of POJOs mapped with JsonBinaryType

Description

When reading entities with collections of POJOs such as TestEntity (see Example 1), updates are performed on the database without the objects being changed.

Example 1

@TypeDefs({
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
@Entity
public class TestEntity {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    public int id;

    @Type(type = "jsonb")
    public Collection<TestElement> testElements;


    public static class TestElement implements Serializable {

        static final long serialVersionUID = -8485895696327472738L;

        public String testProperty;

    }

}

Analysis

It appears as if the dirty check on JsonBinaryType properties for collections of POJOs does no longer work as expected. The problem occurs since [commit-1]. This commit introduces a new dirty checking behavior for JsonBinaryType, when used with collections:

public boolean areEqual(Object one, Object another) {
// ...
	if (one instanceof Collection && another instanceof Collection) {
		return Objects.equals(one, another);
	}

JsonTypeDescriptor

I would not expect the usage of java.lang.Object#equals at this point. In my understanding it creates an inconsistency compared to the dirty checking behavior used for non-collection types. The recursive field by field comparison before [commit-1] is appropriate in my opinion.

Workaround

Downgrade to the version before [commit-1]: version 2.4.2.

Links

commit-1

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@zxmarcos All JPA attributes should be Serializable. The specification requires that, as indicated by the Hibernate documentation.

to clarify - by “entity” we mean JPA Entity, by POJO - the type, that is persisted as JSON column, right ?

So, in this case:

@Entity
@Table(name = "RA_OUTBOX_RECORDS")
@Getter @Setter @ToString
@TypeDef(
        name = "jsonb",
        typeClass = JsonBlobType.class
)
public class DocumentOutboxRecord {
///
    @Column(name = "PAYLOAD")
    @Type(type = "jsonb")
    DocumentPayloadV10 payload;
///

DocumentOutboxRecord would be an “entity” and DocumentPayloadV10 - a POJO?

and by “implementing equals/hashCode for POJO attributes” we mean that all custom (non-JDK) types, used inside the POJO type, and their attributes in turn, all should have equals/hashCode implemented (i.e. not inherited from Object) ?

well, if that is correct, then “POJO” and the only custom type used in the POJO definition, they both have equals and hashCode implementations, generated with Lombok.

And by the way, I have narrowed down the problem starting point: it was in 2.9.3 version of “hibernate-types-52” artifact. So, with 2.9.0/2.9.1/2.9.2 all is fine (just as with 2.5.2, for example), and with 2.9.3. and all subsequent versions - all goes bust… Version number being the only difference

Thanks. I’ll review it when I have some time. It would be great if there were a Contribution Guide too. You can supply one if you like.