Issue Type: Bug Bug
Affects Versions: 4.3.0.Beta2, 4.2.1
Assignee: Unassigned
Components: envers
Created: 12/Jun/13 3:13 AM
Description:

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:

A.java
@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
}
B.java
@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 DDL
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;
Aud JoinTable DDL
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 Output
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

Environment: PostgreSQL 8.4
Project: Hibernate ORM
Labels: envers hibernate-envers
Priority: Major Major
Reporter: Hugo Espresati
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira