[英]Javers - DiffIgnore on bidirectional OneToMany
我正在嘗試將Javers與Spring Data REST項目集成。 目前,我在域中擁有以下實體。
Student.class
@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文檔中提到:
在現實世界中,域對象通常包含各種您不想審核的嘈雜屬性,例如動態代理(例如Hibernate延遲加載代理),重復數據,技術標志,自動生成的數據等。
這是否意味着我把@DiffIgnore
在@ManyToOne
學生領域的聯系號碼類或@OneToMany
在學生班級通訊錄領域?
這取決於您如何記錄對象以及要記錄的內容。 考慮這兩行(假設您在p和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。 您可以使用它代替這些對象之間的鏈接。
更新:
查看您的模型,建議您刪除student
屬性。 從電話號碼鏈接到學生沒有任何意義。 給學生分配了一個號碼,而不是相反。 因此,您的模型將如下所示。
@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;
}
如果您確實需要查找以電話號碼開頭的人/學生,則可以為您的人類創建一個存儲庫,以便您進行搜索。 看起來像這樣:
//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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.