【问题标题】:Possible bug in Envers auditing OneToOne with JoinTable. Confirm?Envers 使用 JoinTable 审核 OneToOne 中可能存在的错误。确认?
【发布时间】:2013-06-07 05:09:40
【问题描述】:

我使用的是 Hibernate 和 Envers 4.2.1.Final 版本。我要确认的问题是,当没有建立关系时,Envers 正在尝试将数据插入可审计的可连接项中。我有一个像这样映射的 OneToOne 双向关系:

实体 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
}

实体 B:

@Entity
@Audited
public class B {

    @Id
    @GeneratedValue
    private Long id;

    @OneToOne(mappedBy = "b", optional=true)
    private A a = null;

    // Getters + Setters + HashCode + Equals 
}

JoinTables 生成的 DDL 是:

加入表 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(用于 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;

在创建 A 实体并将其持久化时,SQL 语句为:

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
...

如您所见,Envers 试图在 A 实例中未建立任何关系(初始化为空)并且为 Envers 生成的架构具有 Not Null 限制时,在可连接的 aud 上插入一个条目。有些人可以在打开 JIRA 之前确认此行为?

【问题讨论】:

  • 是的,这看起来像一个错误。
  • 好的!我将创建一个 JIRA。谢谢

标签: hibernate hibernate-envers


【解决方案1】:
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 2013-02-26
  • 2016-10-27
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多