Envers:没有附加审核表的单向OneToMany吗? [英] Envers: Unidirectional OneToMany without additional audit table?

查看:71
本文介绍了Envers:没有附加审核表的单向OneToMany吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下数据库架构:

Employee [EMP_ID(PK),姓名,薪水]

Employee[EMP_ID (PK), name, salary]

电话[ID(PK),number_str,OWNER_ID(FK)]

Phone[ID (PK), number_str, OWNER_ID (FK)]

Employee_aud [EMP_ID(PK),REV(PK/FK),REVTYPE,姓名,薪水]

Employee_aud[EMP_ID (PK), REV (PK/FK), REVTYPE, name, salary]

Phone_aud [ID(PK),REV(PK/FK),REVTYPE,number_str]

Phone_aud[ID (PK), REV (PK/FK), REVTYPE, number_str]

Employe_phone_aud [REV(PK/FK),OWNER_ID(PK/FK),REVTYPE(PK/FK)]

Employe_phone_aud[REV(PK/FK), OWNER_ID(PK/FK), REVTYPE(PK/FK)]

可以用以下Java实体表示:

can be expressed with the following Java Entities:

员工:

@Entity
@Audited
public class Employee {

    @Id
    @GeneratedValue
    @Column(name = "EMP_ID")
    private long id;

    @Column
    private String name;

    @Column
    private int salary;

    @OneToMany
    @JoinColumn(name = "OWNER_ID", referencedColumnName = "EMP_ID")
    private final List<Phone> phones = new ArrayList<Phone>();

    public Employee(final String name, final int salary) {
        this.name = name;
        this.salary = salary;
    }

    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(final int salary) {
        this.salary = salary;
    }

    public void addPhone(final Phone phone) {
        this.phones.add(phone);
    }
}

电话:

@Entity
@Audited
public class Phone {

    @Id
    @GeneratedValue
    private long id;

    @Column(name = "number_str")
    private String number;

    public Phone(final String number) {
        this.number = number;
    }

    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(final String number) {
        this.number = number;
    }
}

如您所见,审核表之间有一个链接表,但实体表之间没有任何链接表.在《 Hibernate-Envers开发人员指南》中,我找到了以下文本:

As you can see, there is a linking table between the auditing tables, but no linking table between the entity tables. In the Hibernate-Envers Developerguide I found the following text:

使用这两个(@ OneToMany + @ JoinColumn)批注映射集合时,Hibernate不会生成联接表.但是,Envers必须这样做,以便在阅读相关实体已更改的修订时,不会得到错误的结果.

When a collection is mapped using these two (@OneToMany+@JoinColumn) annotations, Hibernate doesn't generate a join table. Envers, however, has to do this, so that when you read the revisions in which the related entity has changed, you don't get false results.

这是我对本文的解释: 我的员工实体可能属于许多电话实体.如果我将电话添加到某个员工,则我的员工将被修改,因此必须进行审核.使用上面的映射,这将导致电话实体而不是员工的审核条目.使用链接表可以解决此问题(因为该表描述了员工拥有的电话集合中的更改).

This is my interpretation of this text: My Employee Entity may belong to many phone entities. If I e.g add a Phone to a certain Employee then my Employee gets modified and thus an audit entry has to be made. Using the mapping above, this would result into an audit entry for the Phone Entity and not for the Employee. This problem is solved using the linking table (as this table describes the changes within the phone-collection owned by the employee).

现在我的3个问题: -我是否理解上述说法正确,还是缺少一些特别之处? -如果Envers没有创建此链接表,则还可以通过查看phone_aud表来跟踪这些关系.这是真的? -是否可以配置/扩展Envers以支持此行为?

Now my 3 questions: - Do I understand the above statement correct or am I missing something special? - If Envers didn't create this linking table, it would also be possible to keep track of these relations by looking at the phone_aud table. Is this true? - Is it possible to configure / extend envers to support this behavior?

注意:我问这个问题仅仅是因为我想知道是否有可能摆脱多余的链接表并更好地理解为什么需要它.

Note: I am asking the question just because I want to know whether it is possible to get rid of the extra linking table and to get a better understanding why it is needed.

谢谢!

推荐答案

  1. 是的,您正确理解了文档中的说明
  2. 是的,但是阅读电话"的更改历史记录将为您提供相同的后续对象(因为只有emp_id会更改)
  3. 否,当前无法更改此行为.除非你将您的映射更改为双向一对多关系

这篇关于Envers:没有附加审核表的单向OneToMany吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆