如果通过添加新实体来更改@oneToMany-实体,则修订版将包含其他字段的空值

时间:2019-06-13 13:59:04

标签: hibernate-envers

为一个人添加新地址时,应创建一个新修订。创建了修订,但是修订中实体的其余字段标记为空。

不同和正确: 当我更改一个人的名字时,将创建一个修订版本,并在其中输入所有字段。

人员实体:

@Entity
@Table(name = "Person")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Audited
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @OneToMany(mappedBy="person")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Address> addresses = new HashSet<>();

人员审核实体:

@Entity
@Table(name = "person_aud")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class PersonAud implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private AuditIdentity auditIdentity;

    @Column(name = "revtype")
    private Short revtype;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @ManyToOne
    @MapsId("auditIdentity.id")
    @JoinColumn(name = "id", nullable = false)
    private Person person;

    @OneToMany
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Address> addresses = new HashSet<>();

地址实体:

@Entity
@Table(name = "address")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Audited
public class Address implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;

    @Column(name = "street")
    private String street;


    @Column(name = "house_number")
    private String houseNumber;

    @Column(name = "zip_code")
    private String zipCode;

    @Column(name = "city")
    private String city;

    @Column(name = "state_province")
    private String stateProvince;

    @Column(name = "country")
    private String country;

    @ManyToOne
    @JsonIgnoreProperties("addresses")
    private Person person;

    @OneToOne
    @JsonIgnoreProperties("addresses")
    @NotAudited
    private PersonAud personAud;

如果我添加一个属于个人XY的新地址,则我的表格如下所示:

PERSON_AUD: 编号:1 修订版:1001 版本:1 FIRST_NAME:NULL LAST_NAME:NULL

例如,如果我更改名字,则输入first_name和last_name的字段。

1 个答案:

答案 0 :(得分:0)

问题解决了(不是很完美)

我更改了行:

@OneToMany(mappedBy="person")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Address> addresses = new HashSet<>();

收件人:

@OneToMany(cascade = {CascadeType.ALL})
        @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
        private Set<Address> addresses = new HashSet<>();

现在该过程的工作方式如下:

  1. 添加一个具有空地址数组的新人

  2. 添加引用该个人ID的新地址

  3. 在此Person上执行类似PUT的操作,以分配地址对象。

= PERSON_AUD表将包含修订和字段。

问题:  这并不完美,因为您必须手动将地址分配给此人。

还有其他可能的解决方法吗?