Hibernate – Envers: Unidirectional OneTomany does not have additional audit tables?

The following database structure:

Employee [EMP_ID(PK), name, salary]

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

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

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

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

The following Java can be used Entity representation:

Employee:

@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 phones = new ArrayList();

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() {< br /> 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);
}
}

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(fina l long id) {
this.id = id;
}

public String getNumber() {
return number;
}

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

As you can see, there are audit tables Link tables, but there is no link table between entity tables. In the Hibernate-Envers Developerguide, I found the following text:

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.< /p>

This is my explanation of this article:
My employee entity may belong to many phone entities. If I add a phone call to an employee, for example, my employee will be modified, So it must be audited. Using the mapping above, this will result in audit entries for phone entities, rather than audit entries for employees. Using a linked table solves this problem (because this table describes changes in the collection of phones owned by employees). /p>

Now my 3 questions:
-Do I understand the above statement is correct or am I missing something special?
– If Envers did not create this link table, you can also track these relationships 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 this question only because I want to know if it is possible to get rid of the extra link list and better understand why it is needed.

Thank you!

>Yes, you understand the statement in the document correctly>Yes, but read “Phone” The changed history will provide you with the same subsequent objects (because only emp_id will change)> No, this behavior cannot be changed at this time. Unless you, for example, change the mapping to a two-way one-to-many relationship

The following database schema:

Employee [EMP_ID(PK), name, salary]

Phone [ID(PK),number_str,OWNER_ID(FK)]< /p>

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

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

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

It can be represented by the following Java entities:

< p>Employee:

@Entity
@Audited
public class Employee {

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

@Column
private String name;

@Column< br /> private int salary;

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

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;
}< br />
public void addPhone(final Phone phone) {
this.phones.add(phone);
}
}

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

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

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 explanation of this article:
My employee entity may belong to many phone entities. If I add a phone to an employee, for example, my employee will be modified, so it must Perform an audit. Using the mapping above, this will result in audit entries for phone entities, rather than employees’ audit entries. Using a linked table solves this problem (because this table describes changes in the collection of phones owned by employees).

Now my 3 questions:
-Do I understand the above statement is correct or am I missing something special?
– If Envers did not create this link table, you can also track these relationships 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 this question only because I want to know if it is possible to get rid of the extra link list and better understand why it is needed.

Thank you!

>Yes, you understand the statement in the document correctly>Yes, but reading the history of “phone” changes will provide you with the same follow-up objects (Because only emp_id will be changed)> No, currently this behavior cannot be changed. Unless you, for example, change the mapping to a two-way one-to-many relationship

Leave a Comment

Your email address will not be published.