Javers - DiffIgnore on bidirectional OneToMany

ⅰ亾dé卋堺 提交于 2020-01-06 07:24:51

问题


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  {

        @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;

    }

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?


回答1:


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  {

    @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;
}

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.



来源:https://stackoverflow.com/questions/48263340/javers-diffignore-on-bidirectional-onetomany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!