Hibernate-Envers将错误的数据写入AUD列 [英] Hibernate - Envers Writes Wrong Data to AUD Column

查看:90
本文介绍了Hibernate-Envers将错误的数据写入AUD列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到Envers问题.我有两个实体,CITY和CITY_I18N.通过CITY实体的"CODE"列,它们之间存在一对多"关系.当我尝试保存CityI18N实体时,该操作成功结束,但是CITY_I18N_AUD表中的记录包含错误的值,Envers会写入CITY实体的"ID"列,而不是实际的外键"CODE"列.

I am facing a problem with Envers. I have two entities, CITY and CITY_I18N. There is an "One-to-Many" relationship between them, via "CODE" column of CITY entity. When I try to save CityI18N entity, the operation ends successfully, but the record in CITY_I18N_AUD table contains wrong values, Envers writes "ID" column of CITY entity, instead of real foreign key, "CODE" column.

我该如何解决?

谢谢.

@Entity
@Audited
public class City  {

    @Id
    @SequenceGenerator(name = "CITY_ID_GENERATOR", sequenceName = "SEQ_CITY")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CITY_ID_GENERATOR")
    private Long ID;

    @Column(name = "CODE")
    private String code;

    @OneToMany(mappedBy = "city", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    @Fetch(value = FetchMode.SUBSELECT)
    @NotAudited
    private List<CityI18n> cityI18ns;
}

@Entity
@Table(name="CITY_I18N")
@Audited
public class CityI18n {

    @Id
    @SequenceGenerator(name="CITY_I18N_ID_GENERATOR", sequenceName="SEQ_APPLICATION")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CITY_I18N_ID_GENERATOR")
    private Long ID;

    private String name;

    @ManyToOne
    @JoinColumn(name = "LANGUAGE_CODE", referencedColumnName="CODE")
    private Language language;

    @ManyToOne
    @JoinColumn(name = "CITY_CODE", referencedColumnName="CODE")
    private City city;
}

推荐答案

这是一个老问题,但是最近我遇到了同样的问题,并且Envers中似乎仍未解决该问题

This is an old question, but I had the same exact issue recently and it seems it is still not addressed in Envers yet.

我的解决方案是让Envers将 @Id 列中的数据写入 _AUD 表的 referencedColumnName 列中.

My solution was to just let Envers write the data from the @Id column into the referencedColumnName column of the _AUD table.

似乎可以很好地重建关系.

It seems to be able to reconstruct the relationships fine.

唯一的障碍是手动更改 _AUD 表中列的SQL数据类型:

The only obstacle is to manually change the SQL data type of the column in the _AUD table:

CREATE TABLE CITY_AUD (
    ID int8 NOT NULL,
    REV int8 NOT NULL,
    REVTYPE int2,
    REVEND int8,   -- if you are using ValidityAuditStrategy
    CODE varchar(255),
    PRIMARY KEY (ID, REV)
);

CREATE TABLE CITY_I18N_AUD (
    ID int8 NOT NULL,
    REV int8 NOT NULL,
    REVTYPE int2,
    REVEND int8,   -- if you are using ValidityAuditStrategy
    NAME varchar(255),
    CITY_CODE int8, -- Notice the type is int8 here - it will actually hold the CITY.ID not CITY.CITY_CODE
    LANGUAGE_CODE int8, -- assuming it's the same situation for LANGUAGE (wasn't clear from the question)
    PRIMARY KEY (ID, REV)
);

这可能不适用于所有用例,但至少可以保持关系完整.

This might not work for all use cases, but it at least keeps relationship intact.

这篇关于Hibernate-Envers将错误的数据写入AUD列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆