@ShallowReference用例

时间:2017-11-19 10:03:41

标签: java javers

我尝试通过创建测试用例来理解@ShallowReference的目的,但我没有看到Javers更改有任何差异。我的应用程序是一个带有存储库注释的Spring Boot应用程序。

我在CustomerProject之间存在一对多的双向关系。测试用例创建CustomerProject,然后将项目添加到客户。我尝试使用@ShallowReference字段上的Set<Project>运行测试用例,以查看@ShallowReference行为。然后我查询更改,但在两种情况下它们都是相同的。我正在寻找一个测试用例来理解@ShallowReference。

测试用例:

@RunWith(SpringRunner.class)
@DataJpaTest
@Import({JaversSqlAutoConfiguration.class})
public class AuditTest {

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private Javers javers;

    @Autowired
    private CustomerRepository customerRepository;

    @Test
    public void testJavers() {
        Customer customer = new Customer();
        customer.setName("Stackoverflow");
        customerRepository.save(customer);

        Project project = new Project();
        project.setName("Framework");
        customer.addProject(project);

        projectRepository.save(project);

        QueryBuilder jqlQuery = QueryBuilder.anyDomainObject().withNewObjectChanges();
        List<Change> changes = javers.findChanges(jqlQuery.build());
        assertEquals(9, changes.size());

        project.setName("Backend Framework");
        projectRepository.save(project);

        changes = javers.findChanges(jqlQuery.build());
        assertEquals(10, changes.size());

    }
}

客户实体:

@Entity
@Table(name = "CUSTOMER")
@SQLDelete(sql = "UPDATE customer SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false")
public class Customer extends AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "customer")
//    @ShallowReference
    private Set<Project> projects = new HashSet<>();

// Getter/Setter

    public void addProject(Project project) {
        projects.add(project);
        project.setCustomer(this);
    }

    public void removeProject(Project project) {
        projects.remove(project);
        project.setCustomer(null);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Customer customer = (Customer) o;
        return Objects.equals(id, customer.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

项目实体:

@Entity
@Table(name = "PROJECT")
@SQLDelete(sql = "UPDATE project SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false")
@EntityListeners(AuditingEntityListener.class)
public class Project extends AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    private Customer customer;

// Getter/Setter

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Project)) return false;
        Project project = (Project) o;
        return Objects.equals(id, project.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

1 个答案:

答案 0 :(得分:0)

@ShallowReference不适合收藏。我们有一个未解决的问题https://github.com/javers/javers/issues/528 欢迎PR。