Hibernate Enver - Listening for only one change in referenced class

34 Views Asked by At

I have an entity as shown below that I am auditing using Hibernate Enver

@Entity
@Table(name = "watch_item")
@Audited
public class WatchItemEntity implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Type(type = "uuid-char")
  @Column(name = "watch_item_id", columnDefinition = "VARCHAR(36)")
  private UUID watchItemId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "watch_model_id")
  private WatchModelEntity watchModel;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "private_user_id")
  private PrivateUserEntity privateUser;

  private String serialNumber;

  private Integer productionYear;

  private String generalCondition;

  private Boolean isMovementFullyFunctional;

  private Boolean isInOriginalCondition;

  private String comment;

  private Boolean isProofOfPurchaseAvailable;

  private String country;

  private Boolean isCustomsDeclared;

  private Boolean hasPaper;

  private Boolean hasBox;

  private String otherAccessories;

  @CreatedDate private LocalDateTime createdDate;

  @LastModifiedDate private LocalDateTime modifiedDate;

  private String lastServiceProvider;

  private LocalDate lastServiceDate;

  private BigDecimal lastServiceCost;

  private BigDecimal purchasedPrice;

  private LocalDate purchasedOn;
  
  ...

}

As you can see, it has a PrivateUserEntity field. I want Hibernate Envers to record a change when the privateUser changes (and not record changes in PrivateUserEntity that correspond to the privateUser). However, I don't want to create a Private_User_Aud table. To give some context, a WatchItem can only be owned by one PrivateUser and hence, when the PrivateUser field changes, that means that the WatchItem's owner changed. The entity can be seen below

@Entity
@Table(name = "private_user")
public class PrivateUserEntity implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Type(type = "uuid-char")
  @Column(name = "private_user_id", columnDefinition = "VARCHAR(36)")
  private UUID privateUserId;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "user_id")
  private UserEntity user;

  @LastModifiedDate private LocalDate modifiedDate;

  private String title;

  private String firstName;

  private String lastName;

  private String phone;

  private LocalDate dateOfBirth;

  private String email;

  private String gender;

  private String nationality;
  private String residencyPermitType;
  private LocalDate residencyPermitValidSince;

  private String preferredLanguage;

  ...

}

Is this possible? And if so, how?

1

There are 1 best solutions below

0
viking On

You can disable audit for it, doesn't work?

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "private_user_id")
  @NotAudited
  private PrivateUserEntity privateUser;