We have 2 @Entity
s having fields like that:
@Data
@NoArgsConstructor
@Entity
@Audited
@AuditTable(schema = "audit", value = "tariff_option")
@AuditOverride(forClass = BaseEntity.class, isAudited = true)
@Table(name = "tariff_option")
@EqualsAndHashCode(of = {}, callSuper = true)
@ToString()
public class TariffOption extends BaseEntity {
private static final long serialVersionUID = -6398231779406280786L;
...
@ManyToOne
@JoinColumn(name = "dict_tariff_id", updatable = false)
private DictTariff tariff;
}
and
@Data
@NoArgsConstructor
@Entity
@Audited
@AuditTable(schema = "audit", value = "dict_tariff")
@AuditOverride(forClass = BaseEntity.class, isAudited = true)
@Table(name = "dict_tariff")
@EqualsAndHashCode(of = {}, callSuper = true)
@ToString(exclude = {"contractorTypes", "service", "tariffOptions", "dictTariffOptions"})
@JsonIgnoreProperties(value = {"contractorTypes", "service", "tariffOptions", "dictTariffOptions"})
public class DictTariff extends BaseEntity {
private static final long serialVersionUID = -3881580795280130829L;
...
@OneToMany(mappedBy = "tariff", fetch = FetchType.LAZY)
private List<TariffOption> tariffOptions;
}
and then when we save the dictTariffOption
variable of TariffOption
class, the associated dictTariff
of DictTariff
class is saved because of @ManyToOne
annotation and ownership of the tie by the dictTariffOption
:
repository.save(dictTariffOption)
This causes problem that on any change of dictTariffOption
the dictTariff
is saved again with the same values. That causes the new entry in audit scheme what we want to avoid.
I tried EntityManager.detach(dictTariff)
right before saving the dictTariffOption
, but it doesn't help. So what is the approach to ignore the update of the linked entity? I also tried to re-retireve it right before the save to make it not to be JPA-dirty, but it doesn't help and it got updated.
jpa:
generate-ddl: false
hibernate:
ddl-auto: validate
properties:
hibernate:
dialect: rf.dom.billing.model.common.postgres.CustomPostgreSqlDialect
enable_lazy_load_no_trans: true
org:
hibernate:
envers:
default_schema: audit
Per Chris,
public class DictTariff extends BaseEntity {
@OneToMany(mappedBy = "tariff", fetch = FetchType.LAZY)
@NotAudited
private List<TariffOption> tariffOptions;
helped and no longer update the audit changes on DictTariff
when whe TariffOption
, reffering the DictTariff
is saved.