question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected dirty checking behavior on collections of POJOs mapped with JsonBinaryType

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
vladmihalceacommented, Dec 2, 2020

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.

0reactions
vladmihalceacommented, Dec 1, 2021

I don’t know what the @Data or @Jacksonized annotations are. I’m glad you found a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does Hibernate detect dirty state of an entity object?
Hibernate default dirty checking mechanism will match all mapped properties of all currently attached entities against their initial loading-time values ...
Read more >
(Hibernate) JPA Fundamentals - Springer Link
dirty Checking mechanism and it is materialized in an UPDATE triggered at persistence Context flush time. since Hibernate didn't read the latest state....
Read more >
How to map a JSON collection using JPA and Hibernate
How to map a JSON collection using JPA and Hibernate and store it either as a JSON or JSONB column type on MySQL,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found