Consider the following minimal example:
{code:lang=Java}
import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; import org.hibernate.envers.Audited;
@Entity @Audited public class TestEntity implements Serializable {
@Id private Long id;
@ElementCollection private List<String> strings = new ArrayList<>();
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public List<String> getStrings() { return strings; }
public void setStrings(List<String> strings) { this.strings = strings; } } {code}
which leads to the following created DDL created by Hibernate
{code:lang=SQL} CREATE TABLE `testentity` ( `id` bigint(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `testentity_strings` ( `TestEntity_id` bigint(20) NOT NULL, `strings` varchar(255) DEFAULT NULL, KEY `FK6gvnp6uhj6p14qb8jr7w4a4sc` (`TestEntity_id`), CONSTRAINT `FK6gvnp6uhj6p14qb8jr7w4a4sc` FOREIGN KEY (`TestEntity_id`) REFERENCES `testentity` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `testentity_aud` ( `id` bigint(20) NOT NULL, `revision` int(11) NOT NULL, `action` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`,`revision`), KEY `FKtoml4ns3581arnt5f7i1srxai` (`revision`), CONSTRAINT `FKtoml4ns3581arnt5f7i1srxai` FOREIGN KEY (`revision`) REFERENCES `revinfo` (`REV`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `testentity_strings_aud` ( `revision` int(11) NOT NULL, `TestEntity_id` bigint(20) NOT NULL, `strings` varchar(255) NOT NULL, `action` tinyint(4) DEFAULT NULL, PRIMARY KEY (`revision`,`TestEntity_id`,`strings`), CONSTRAINT `FKadlc041c3dxra6fmfxsku0fuh` FOREIGN KEY (`revision`) REFERENCES `revinfo` (`REV`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; {code}
Now, the problem is the primary key of the last table as Envers decided to include all columns of `testentity_strings` in its audit table primary key which can lead to unexpected problems when one of the columns is e.g. 1024 characters long. |
|