Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to integrate Javers with a Spring Data REST project. Currently I have the following entities in my domain.
Student.class
@Entity
public class Person {
@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 {
@GeneratedValue
private Long id;
private String phoneNumber;
private Boolean isPrimary;
@ManyToOne
private Student student;
In the javers docs it is mentioned that:
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.
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?
–
–
–
–
–
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);
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.
UPDATE:
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 {
@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 {
@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);
With this, your model is cleaner and you don't need to use DiffIgnore at all.
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.