Hibernate envers - 加载嵌套对象

时间:2015-08-03 12:40:19

标签: java audit hibernate-envers

我有一个有一些列表的实体,看起来像这样:

@Entity
@Table(name = "HOME")
@Audited
public class House {

    @Id
    private Integer id;

    @Version
    @Column(name = "UPDATE_DATE", nullable = false)
    private Date updateDate;

    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @Cascade(CascadeType.ALL)
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "home", orphanRemoval = true)
    private Set<Room> rooms;

    [...]

}

还假设Room实体看起来像这样:

@Entity
@Table(name = "ROOM")
@Audited
public class Room {

    @Id
    private Integer id;

    @Version
    @Column(name = "UPDATE_DATE", nullable = false)
    private Date updateDate;

    @Column(name = "NAME", nullable = false)
    private String name;

    @Cascade(CascadeType.ALL)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="HOUSE", nullable=false)
    private House house;

    [...]

}

如您所见,房间是通过级联保存的。让我们回到我的问题的核心......

PRECONDITINS:

住宅对象中的当前数据:

House:
    description: "red house"
    rooms: [room1, room2]

审计表中的当前数据:

--- HOUSE_A ---
| REV | REVTYPE | ID | UPDATE_DATE     | DESCRIPTION |
+-----+---------+----+-----------------+-------------+
| 111 | 0       | 10 | 2015-08-3 12:00 | red house   |

--- ROOM_A ---
| REV | REVTYPE | ID  | UPDATE_DATE     | NAME  | HOUSE |
| 111 | 0       | 100 | 2015-08-3 12:00 | room1 | 10    |
| 111 | 0       | 110 | 2015-08-3 12:00 | room2 | 10    |

用户步骤:

使用这些数据更新house对象(更改房屋描述):

House:
    description: "blue house"
    rooms: [room1, room2]

在此操作之后,审计表将如下所示:

--- HOUSE_A ---
| REV | REVTYPE | ID | UPDATE_DATE     | DESCRIPTION  |
+-----+---------+----+-----------------+--------------+
| 111 | 0       | 10 | 2015-08-3 12:00 | red house    |
| 112 | 1       | 10 | 2015-08-3 12:30 | blue house   |

--- ROOM_A ---
| REV | REVTYPE | ID  | UPDATE_DATE     | NAME  | HOUSE |
| 111 | 0       | 100 | 2015-08-3 12:00 | room1 | 10    |
| 111 | 0       | 110 | 2015-08-3 12:00 | room2 | 10    |

使用这些数据更新house对象(不要更改house对象并添加一个房间):

House:
    description: "blue house"
    rooms: [room1, room2, room3]

在此操作之后,审计表将如下所示:

--- HOUSE_A ---
| REV | REVTYPE | ID | UPDATE_DATE     | DESCRIPTION  |
+-----+---------+----+-----------------+--------------+
| 111 | 0       | 10 | 2015-08-3 12:00 | red house    |
| 112 | 1       | 10 | 2015-08-3 12:30 | blue house   |

--- ROOM_A ---
| REV | REVTYPE | ID  | UPDATE_DATE     | NAME  | HOUSE |
| 111 | 0       | 100 | 2015-08-3 12:00 | room1 | 10    |
| 111 | 0       | 110 | 2015-08-3 12:00 | room2 | 10    |
| 113 | 0       | 120 | 2015-08-3 12:40 | room3 | 10    |

LOAD AUDITED HOUSE DATA:

--- current result ---
HOUSE_A(last_rev) -> HOUSE_A(112) -> 'blue house' with room1 and room2

--- expected result ---
HOUSE_A(last_rev) -> HOUSE_A(113) -> 'blue house' with room1, room2 and room3

这是问题......

房屋的最高版本是112,但是我已经完成的最后一次操作已经保存了修订版113(由于房屋对象没有改变,因此没有将转入条目添加到HOUSE_A)。我知道,envers会加载版本较小或相等的house对象的所有数据。在这种情况下,不会加载上一个操作。问题是 - 加载这些类型操作(主要对象未更新)的唯一方法是在保存之前更新主对象(house)的最后更新日期,这样新的条目将添加到HOUSE_A,其版本与ROOM_A相同? / p>

在此“解决方法”之后,审计表将如下所示......

--- HOUSE_A ---
| REV | REVTYPE | ID | UPDATE_DATE      | DESCRIPTION  |
+-----+---------+----+------------------+--------------+
| 111 | 0       | 10 | 2015-08-30 12:00 | red house    |
| 112 | 1       | 10 | 2015-08-30 12:30 | blue house   |
| 113 | 1       | 10 | 2015-08-30 12:40 | blue house   |

--- ROOM_A ---
| REV | REVTYPE | ID  | UPDATE_DATE      | NAME  | HOUSE |
+-----+---------+-----+------------------+-------+-------+
| 111 | 0       | 100 | 2015-08-30 12:00 | room1 | 10    |
| 111 | 0       | 110 | 2015-08-30 12:00 | room2 | 10    |
| 113 | 0       | 120 | 2015-08-30 12:40 | room3 | 10    |

1 个答案:

答案 0 :(得分:0)

您没有提到您正在使用的Envers版本,但是在Envers 5.x中默认启用了一个配置属性,该属性应触发此确切功能:

org.hibernate.envers.revision_on_collection_change=true

由于Room拥有与Home的关系,因此启用此配置会导致Envers在修改Home集合时记录Room的修订更改。