0

I'm currently struggling with Hibernate Envers in combination with Spring Envers support. I have a simple Connection entity that has a uni-directional OneToMany mapping with a set of Parameters. From my application point of view this is a composite aggregation. So the parameters belong to the connection and cannot live without a connection.

Now, my problem is that Hibernate Envers does not create a new revision when I update a parameter of the connection. Is this the expected behavior or am I doing something wrong? I modify a parameter on a connection object and store that object with a spring-data JPA repository. In this case I expect that a new revision for the connection, the n:m mapping table and the respective parameter entity is created in the _AUD tables. Unfortunately only a new revision in the PARMETER_AUD table is created.

Here are my two entities:

@Entity
@Audited 
public class Connection {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "DATABASE_ID", updatable = false, nullable = false)
    protected Long uniqueId;
 
    @Column(name = "DISPLAY_NAME", length = 64, nullable = false)
    private String displayName;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("name ASC")
    private List<Parameter> parameters = new LinkedList<>();
}

@Entity
@Audited 
public class Parameter {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "DATABASE_ID", updatable = false, nullable = false)
    protected Long uniqueId;

    @Column(name = "NAME", length = 128, nullable = false)
    private String name;

    @Column(name = "VALUE", length = 1024)
    private String value;

    public Parameter(String name, String value) {
        this.name = name;
        this.value = value;
    }
}

And here a piece of my unit test case:

@Autowired
private ConnectionRepository repo;
...

Connection connection = new Connection();
connection.setDisplayName("foo");
connection.getParameters().add(new Parameter("parm1", "value1"));

// Initial insert of the connection object
repo.save(connection);

// Now we update a parameter of the connection and expect a new revision for the 
connection.getParameters().get(0).setValue("value2");
repo.save(connection);

The full example can be found at: https://github.com/svesch84/spring-envers-test

Any help/hint would be appreciated.

Sven S.
  • 21
  • 3
  • This is expected behavior. new revision for connection will only be created if the non-relational fields of connection are updated. – Md Zahid Raza Jul 13 '21 at 19:12
  • You want make some modification audit and you don't have any `version` field in your entities ? – Zorglube Nov 24 '21 at 14:24

0 Answers0