I'm using Hibernate and Envers 4.2.1.Final version. The problem I want to confirm is that Envers is trying to insert data in the audit jointable when there's no relation stablished. I have a OneToOne bidirectional relation maped like this:
Entity A:
@Entity
@Audited
public class A {
@Id
@GeneratedValue
private Long id;
@OneToOne(optional = true)
@JoinTable(name = "A_B", joinColumns = { @JoinColumn(name = "a_id", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "b_id") })
private B b = null;
// Getters + Setters + HashCode + Equals
}
Entity B:
@Entity
@Audited
public class B {
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "b", optional=true)
private A a = null;
// Getters + Setters + HashCode + Equals
}
The generated DDL of the JoinTables are:
JoinTable A_B:
CREATE TABLE public.a_b (
b_id BIGINT,
a_id BIGINT NOT NULL,
CONSTRAINT a_b_pkey PRIMARY KEY(a_id),
CONSTRAINT fk_32f8f6e4ba7d48728bfb376dd19 FOREIGN KEY (a_id)
REFERENCES public.a(id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE,
CONSTRAINT fk_6c0d47b64ad3462aaab3813ccae FOREIGN KEY (b_id)
REFERENCES public.b(id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE
) WITHOUT OIDS;
JoinTable A_B_AUD (for Envers):
CREATE TABLE public.a_b_aud (
b_id BIGINT NOT NULL,
rev INTEGER NOT NULL,
a_id BIGINT NOT NULL,
CONSTRAINT a_b_aud_pkey PRIMARY KEY(a_id, rev),
CONSTRAINT fk_5689e745b66c4ee2a8822e44079 FOREIGN KEY (b_id, rev)
REFERENCES public.b_aud(id, rev)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE,
CONSTRAINT fk_b5c868c4f5f34d35bdb7a6c1281 FOREIGN KEY (a_id, rev)
REFERENCES public.a_aud(id, rev)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE
) WITHOUT OIDS;
When creating an A entity and persisting it, the SQL statements are:
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into A (id) values (?)
12:58:50,488 TRACE BasicBinder:84 - binding parameter [1] as [BIGINT] - 21
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into revinfo (timestamp, usuarioId, id) values (?, ?, ?)
12:58:50,506 TRACE BasicBinder:84 - binding parameter [1] as [BIGINT] - 1370948330497
12:58:50,506 TRACE BasicBinder:72 - binding parameter [2] as [BIGINT] - 0
12:58:50,507 TRACE BasicBinder:84 - binding parameter [3] as [INTEGER] - 22
Hibernate: insert into A_AUD (REVTYPE, id, REV) values (?, ?, ?)
12:58:50,509 TRACE BasicBinder:84 - binding parameter [1] as [INTEGER] - 0
12:58:50,509 TRACE BasicBinder:84 - binding parameter [2] as [BIGINT] - 21
12:58:50,510 TRACE BasicBinder:84 - binding parameter [3] as [INTEGER] - 22
Hibernate: insert into A_B_AUD (b_id, a_id, REV) values (?, ?, ?)
12:58:50,514 TRACE BasicBinder:72 - binding parameter [1] as [BIGINT] - <null>
12:58:50,515 TRACE BasicBinder:84 - binding parameter [2] as [BIGINT] - 21
12:58:50,515 TRACE BasicBinder:84 - binding parameter [3] as [INTEGER] - 22
12:58:50,518 WARN SqlExceptionHelper:145 - SQL Error: 0, SQLState: 23502
12:58:50,518 ERROR SqlExceptionHelper:147 - ERROR: null value for the column «b_id» violates not null restriction
...
As you can see Envers is trying to insert an entry on the aud jointable when no relation is stablished (null initialiced) in A instance and schema generated for Envers has a Not Null restriction. Some can confirm this behaviour before opening a JIRA?
I have created a JIRA ticket - Envers auditing OneToOne inserts null in Aud JoinTable in a not stablished relationship