0

我目前正在与 Hibernate Envers 和 Spring Envers 支持相结合。我有一个简单的 Connection 实体,它具有带有一组参数的单向 OneToMany 映射。从我的应用程序的角度来看,这是一个复合聚合。所以参数属于连接,没有连接就无法生存。

现在,我的问题是当我更新连接的参数时,Hibernate Envers 不会创建新的修订版。这是预期的行为还是我做错了什么?我修改了连接对象上的参数,并将该对象与 spring-data JPA 存储库一起存储。在这种情况下,我希望在 _AUD 表中创建连接的新版本、n:m 映射表和相应的参数实体。不幸的是,仅在 PARMETER_AUD 表中创建了一个新版本。

这是我的两个实体:

@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;
    }
}

这是我的一个单元测试用例:

@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);

完整示例可在以下位置找到: https ://github.com/svesch84/spring-envers-test

任何帮助/提示将不胜感激。

4

0 回答 0