我正在评估 javers 以将其用于审计实体。我有一个带有 ValueObjects 嵌套集合的实体。我希望 valueobject 上的每个属性更改都会生成 Entity 的快照。仅当将 valueobject 添加到集合中时才会创建快照。在我的情况下,我向集合中添加了两个 valueobjects创建了实体的两个快照。第三次我只是更改了值对象的属性,而 javers 并没有将其识别为实体的更改,而是为内部值对象创建了快照。
我的问题是我的假设是否有效,或者跟踪集合中值对象的更改的最佳方法是什么
下面是我使用 spring boot 创建的一个简单测试的代码。
我正在使用 javers 3.2.0 版
我的实体如下
package com.example.javersdemo;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.Data;
import org.javers.core.metamodel.annotation.TypeName;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import java.util.List;
@Data
@Entity
@TypeName("User")
class User{
@Id
private String name ;
@JsonUnwrapped
@ElementCollection(targetClass = Hobby.class)
@CollectionTable(name = "USER_HOBBIES")
@JoinColumn(name = "NAME")
private List<Hobby> hobbies;
private User(){
}
public User(String name, List<Hobby> hobbies) {
this.name = name;
this.hobbies = hobbies;
}
}
值对象如下
package com.example.javersdemo;
import lombok.Data;
import javax.persistence.Embeddable;
@Data
@Embeddable
public class Hobby {
private String hobby;
private boolean active;
private Hobby() {
}
public Hobby(String hobby, boolean active) {
this.hobby = hobby;
this.active = active;
}
}
我的弹簧数据存储库如下
package com.example.javersdemo;
import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.springframework.data.repository.CrudRepository;
@JaversSpringDataAuditable
interface TestUserRepository extends CrudRepository<User,String> {
}
下面是我创建的一个 spock 集成测试,用于验证对集合内对象属性的更改会创建一个新快照。
package com.example.javersdemo
import org.javers.core.Javers
import org.javers.repository.jql.QueryBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Specification
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class JaversInnerValueObjectsTest extends Specification {
@Autowired
TestUserRepository userRepository
@Autowired
Javers javers
def 'should create 6 snapshots'() {
given:
def hobbies = [new Hobby('Reading books', true)]
def user = new User('John', hobbies)
when:
userRepository.save(user)
hobbies = [new Hobby('Reading books', true), new Hobby('Watching Soccer', true)]
user.hobbies = hobbies
userRepository.save(user)
hobbies = [new Hobby('Reading books', true), new Hobby('Watching Soccer', false)]
user.hobbies = hobbies
userRepository.save(user)
then:
QueryBuilder jqlQuery = QueryBuilder.byInstanceId('John', User)
def snapshots = javers.findSnapshots(jqlQuery.withChildValueObjects().build())
snapshots.size() == 6
}
}
并且测试失败并出现以下错误
不满足条件:
快照.size() == 6 | | | | 5 false [Snapshot{commit:3.0, id:User/John#hobbies/1, version:2, (hobby:Watching Soccer)}, Snapshot{commit:2.0, id:User/John#hobbies/1, version:1 , (active:true, hobby:Watching Soccer)}, Snapshot{commit:2.0, id:User/John, version:2, (hobbies:[User/John#hobbies/0, User/John#hobbies/1], name:John)}, Snapshot{commit:1.0, id:User/John#hobbies/0, version:1, (active:true, hobby:Reading books)}, Snapshot{commit:1.0, id:User/John,版本:1,(爱好:[用户/John#hobbies/0],姓名:John)}]
预期:6
实际:5