As far as envers is concerned, the basic use case is to record the complete audit of an entity(the parameters we wish to via @Audited annotation). For the case which you are mentioning, new entities might be added properly but for existing it would give issue as there is no revision present in the audit table.
Let's solve the case with the help of a scenario :
Let's say the entity we have taken into consideration is users. The table which gets created now to observe the history, let's say, is users_audit. In addition to it revinfo also would be in place to observe and record all the changes wrt to a given record.
The issue in the first place comes, because whenever there is an update, the persistence layer is unable to find a revision record. So to fix it all the existing entries need to be present in the table and the foreign key mapping with the revinfo table should not break. Hence, two things are required to be done :
- Insert the temp values in the revinfo table so that the rev can act as a foreign key
- Copy the data from the users table to the users_audit table.
Sample Liquibase file can be like this :
CREATE TABLE `revinfo` (
`rev` int(11) NOT NULL AUTO_INCREMENT,
`revtstmp` bigint(20) DEFAULT NULL,
PRIMARY KEY (`rev`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `revinfo` (`revtstmp`) select updated_at from users u;
SET @position := 0;
insert into users_audit (
rev,
revtype,
id,
name,
type,
mobile_number,
password,
parent_id,
profile_image_uri,
is_active,
created_at,
updated_at
) select @position := @position +1, 0,
id,
name,
type,
mobile_number,
password,
parent_id,
profile_image_uri,
is_active,
created_at,
updated_at from us