Javers-双向OneToMany上的DiffIgnore [英] Javers - DiffIgnore on bidirectional OneToMany
问题描述
我正在尝试将Javers与Spring Data REST项目集成.目前,我在域中拥有以下实体.
I am trying to integrate Javers with a Spring Data REST project. Currently I have the following entities in my domain.
学生班
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private Long dob;
@OneToOne
private Gender gender;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student", orphanRemoval = true)
private List<ContactNumber> contactNumbers = new ArrayList<>();
}
ContactNumber.class
@Entity
public class ContactNumber {
@Id
@GeneratedValue
private Long id;
private String phoneNumber;
private Boolean isPrimary;
@ManyToOne
private Student student;
}
在javers文档中提到:
In the javers docs it is mentioned that:
在现实世界中,领域对象通常包含各种类型的噪声您不想审核的属性,例如动态代理(例如Hibernate延迟加载代理),重复数据,技术标志,自动生成的数据,等等.
In the real world, domain objects often contain various kind of noisy properties you don’t want to audit, such as dynamic proxies (like Hibernate lazy loading proxies), duplicated data, technical flags, auto-generated data and so on.
这是否意味着我在联系电话号码类别的 @ManyToOne
学生字段或 @OneToMany
联系人字段中输入了 @DiffIgnore
在学生班上?
So does that mean I put a @DiffIgnore
on the @ManyToOne
student field in the contact number class or the @OneToMany
contacts field in the student class?
推荐答案
这取决于您如何记录对象以及要记录的内容.考虑这两行(假设您在p和contactNumber之间有链接)
It depends how you're logging the objects and what you want to log. Consider these two lines (suppose that you have a link between p and contactNumber)
//This logs p and contactNumber as part of the array part of p. If you want to ignore contactNumber here,
//add @DiffIgnore on the @OneToMany property. If you want to ignore
javers.commit("some_user", p);
//This would log contactNumber and p as the student. You can add @DiffIgnore here on the student property (@ManyToOne)
javers.commit("some_user", contactNumber);
请注意,还有另一个注释 @ShallowReference
,它将记录对象的ID,而不是记录整个对象.例如.如果将@ShallowReference添加到 student
属性,它将不会记录整个Person对象,而只会记录其ID.您可以使用它代替这些对象之间的链接.
Note that there is another annotation @ShallowReference
that will log the id of the object instead of logging the entire object. E.g. if you add @ShallowReference to the student
property it will not log the entire Person object, but only its ID. You can use that instead to have a link between those objects.
更新:
查看您的模型,建议您删除 student
属性.从电话号码链接到学生没有任何意义.给学生分配了一个号码,而不是相反.因此,您的模型将如下所示.
Looking at your model, I'd recommend that you remove the student
property. It doesn't make sense to have a link to the student from the phone number. The student has a number assigned, not the other way around. Thus your model would look like this.
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private Long dob;
@OneToOne
private Gender gender;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student", orphanRemoval = true)
private List<ContactNumber> contactNumbers = new ArrayList<>();
}
ContactNumber.class
ContactNumber.class
@Entity
public class ContactNumber {
@Id
@GeneratedValue
private Long id;
private String phoneNumber;
private Boolean isPrimary;
}
如果您确实需要查找以电话号码开头的人员/学生,则可以为您的人员类创建一个存储库,以便您进行搜索.看起来像这样:
If you really need to find a Person/Student starting with a phone number you can have a Repository for your Person class that enables you to do that search. That would look like this:
//extend from the corresponding Spring Data repository interface based on what you're using. I'll use JPA for this example.
interface PersonRepository extends JpaRepository<Person, Long> {
Person findByPhoneNumber(String phoneNumber);
}
有了这个,您的模型就更整洁了,根本不需要使用DiffIgnore.
With this, your model is cleaner and you don't need to use DiffIgnore at all.
这篇关于Javers-双向OneToMany上的DiffIgnore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!