In this scenario :
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;
}
}
Hibernate creates two tables as expected:
CREATE TABLE testentity (
id bigint(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and
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;
Envers also creates two tables :
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;
and
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;
The problem is with the second audit table (testentity_strings). It sets a constraint (NOT NULL) on column 'strings' even though in the main table strings allows null.
My business requirement is to allow null strings. How can I override this behavior in envers?