5

I have entities with unidirectional ManyToMany relations. A document can be associated to multiple facilities and multiple floors. If a document is updated, the facilities and floors associated with the document should create a new revision in envers as the collection is modified. Unfortunately this does not happen. The question is how do I propagate the changes in the child entity (document) to the owning side of the many to many relationship (facility, floor)?

Information about used libraries:

  • SPRING BOOT 2.0.5.RELEASE
  • SPRING BOOT DATA JPA 2.0.5.RELEASE
  • HIBERNATE 5.2.17.Final
  • HIBERNATE ENVERS 5.2.17.Final
@Entity
@Table(name = "facility")
@Audited(targetAuditMode = RelationTargetAuditMode.AUDITED)
public class Facility extends AbstractAuditingEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    // more attributes

    @ManyToMany
    @JoinTable(name = "facility_documents",
        joinColumns = @JoinColumn(name = "facilities_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "documents_id", referencedColumnName = "id"))
    @Audited(targetAuditMode = AUDITED)
    private Set<Document> documents = new HashSet<>();

    // getter + setter
}
@Entity
@Table(name = "floor")
@Audited(targetAuditMode = RelationTargetAuditMode.AUDITED)
public class Floor extends AbstractAuditingEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    // more attributes

    @ManyToMany
    @JoinTable(name = "floor_documents",
        joinColumns = @JoinColumn(name = "floors_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "documents_id", referencedColumnName = "id"))
    @NotAudited
    private Set<Document> documents = new HashSet<>();

}
@Entity
@Table(name = "document")
@Audited(targetAuditMode = RelationTargetAuditMode.AUDITED)
public class Document extends AbstractAuditingEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    // more attributes, getters and setters but no ManyToMany Mapping to facility and floor

}

The following envers audit table entries are written with the following procedure:

1. STATE: Created facility + document, updated the facility by adding the document to it

enter image description here

2. STATE: Deleted the document

enter image description here Expected: new revision is created for facility Actual: No revision is created for facility

3. STATE: Manually updated facility

enter image description here

If I now fetch the current changelog of the facility, the document is no longer in the revision and is marked as deleted from the facility. This update should happen automatically by changing the document.

SimonH
  • 140
  • 2
  • 17

0 Answers0