我正在使用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是:
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(适用于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
...
正如您所看到的,当Avers中没有建立关系(初始化为null)时,Envers正试图在aud jointable上插入一个条目,而为Envers生成的模式有一个Not Null限制。有些人可以在打开JIRA之前确认这种行为吗?
答案 0 :(得分:0)