AuditQuery如何加入entites

时间:2018-06-18 07:09:43

标签: java hibernate hibernate-envers

我有两个实体:

@Entity
@Getter
@Setter
@SequenceGenerator(name = "SEQ_STORE", sequenceName = "TABLE_A_SEQ", allocationSize = 1)
@Audited
@AuditTable("TABLE_A_ARCH")
@Table(name = "TABLE_A")
public class TableAEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
    protected Long id;

    ..some fields

    @AuditJoinTable(name = "TABLE_B_ARCH")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tableA", orphanRemoval = true)
    @NotEmpty
    private Set<TableBEntity> dataB = Sets.newHashSet();

}

@Entity
@Getter
@Setter
@SequenceGenerator(name = "SEQ_STORE", sequenceName = "TABLE_B_SEQ", allocationSize = 1)
@Audited
@AuditTable("TABLE_B_ARCH")
@Table(name = "TABLE_B")
public class TableBEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
    protected Long id;

    ..some fields

    @ManyToOne
    @JoinColumn(name = "table_a_id", nullable = false)
    private TableAEntity tableA;

}

当我对此进行操作时,所有关于该操作的信息都会转到归档表。我想从相关实体 TableBEntity

中选择所有历史记录更改
AuditReader auditReader = AuditReaderFactory.get(getEntityManager());

AuditQuery auditQuery = auditReader.createQuery()
    .forRevisionsOfEntity(TableAEntity.class, false, true)
    .traverseRelation("dataB", JoinType.INNER, "data_b")
    .addOrder(AuditEntity.revisionNumber().desc());

我尝试了不同的连接类型,但我仍然收到错误:

  

org.hibernate.envers.exception.AuditException:此类关系(..package..TableAEntity.dataB)不受支持,不能用于查询。

当我删除 .traverseRelation(&#34; dataB&#34;,JoinType.INNER,&#34; data_b&#34;)时,我只会从TableAEntity获得更改,而忽略TableBEntity的更改

1 个答案:

答案 0 :(得分:1)

这是因为Envers中关系遍历的初始传递仅适用于 to-one 关联映射;因此,你为什么会得到这个错误。

计划是在Hibernate Envers 6.0中引入对 to-many 关联的支持,请参阅HHH-11735