postgresqlspring-boothibernate

Spring boot hibernate generates update statements when I only select the entity from the DB


I have the following transactional function that's called directly in route post handler and nothing more there.

@Transactional(readOnly = false)
public boolean findInsideTransaction(long titleId, Package parsedDcpPackage, String site) {
    Site siteEntity = siteRepository.findByName(site)
                .orElseThrow(() -> new ResourceNotFoundException(Site.class));
    return true;
    // More insertion logic needs to be written
}

Here is the entity

@Getter
@Setter
@Entity
@Table(name = "site", schema = "public")
public class Site extends BaseEntity {
    private String name;


    @JdbcTypeCode(SqlTypes.JSON)
    @Audited
    @Column(name = "configuration", columnDefinition = "jsonb")
    private SiteConfiguration configuration;
}

Also I utilize

I find that I have update public.site set configuration=?,name=? where id=? traced to this transaction.

I tried the following

I am a bit confused on what may make it behave like that. Any cues what else I should do ?

Versions:


##Update #1 I have found the solution, it's to add @Type annotation to the jsonb column with Vlad json binary @Type(JsonBinaryType.class).

Yet I don't understand what the fix actually did or why even the problem occurred.


Solution

  • I found the issue, it was hibernate's dirty checking mechansim.
    The equals and hashCode were not correctly implemented for SiteConfiguration .

    So hibernate thinks it was changed while it was not, leading into an unnecessary update.