添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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?

What are you trying to achieve? What is it that you want to be ignored? e.g. do you want the ContactNumber object to never be logged/compared or do you want it to be ignored only when it's part of a Person? – Stef Jan 16, 2018 at 13:40 The thing is whenever I add a contact number to a student Javers loads the whole Student entity because there is a ManyToOne reference of Student in ContactNumber class. This executes a lot queries which slows down updates. I have actually simplified the Student class for the question but the class is a lot bigger. – Charlie Jan 16, 2018 at 13:58 I dont want to completely ignore the student reference as I want to know whenever a new contact number is added to the student – Charlie Jan 16, 2018 at 13:59 It's not a direct answer to your question, but why would a contact number know the corresponding student. Conceptually thinking, the contact number is just a number, it shouldn't know anything other than the number. It makes sense that a person/student has a list of contact numbers (e.g. the number is associated to a student), but not the other way around. By cleaning up your model, modeling Javers would be easier as well, while you'd have a better model too. I'll answer the JaVers question below though, but I'd recommend having a clean domain model independently on how/if you're using JaVers. – Stef Jan 16, 2018 at 19:52 You are right Stef, in terms of modeling, Students is a Entity and ContactNumber is a ValueObject owned by Student. ValueObject should not have back-references to master Entity. – Bartek Walacik Jan 16, 2018 at 20:46

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.

Thank you for the answer. If I add @ShallowReference to student property is it possible to get a history of the contact numbers for a particular student from the audit logs? – Charlie Jan 16, 2018 at 15:14 See the answer/suggestion above, I'd recommend to remove the student property altogether and replace it with a Repository finder - PersonRepository.findByContactNumber(String number) - maybe if you really need to. – Stef Jan 16, 2018 at 19:54 The @ShallowReference means that when that property is to be logged, it will only log the ID. It really depends on how you log the objects. If you log the Person, then you should be able to see how numbers have changed from one save to another (id-s in the contactNumbers field will be change and then you can check the numbers yourself). If you commit the ContactNumber, then you should be able to get a history of students that that number has been assigned to. So, technically you should be able to find the numbers assigned to a student at a specific time, though it's not very intuitive. – Stef Jan 16, 2018 at 20:00 I actually had the domain model as you mentioned but according to a blog post by Vlad Mihalcea he says unidirectional onetomany is very inefficient event with @JoinColumn. So I am really confused how I should model this relationship – Charlie Jan 21, 2018 at 15:12 Vlad is right, that is inefficient. But a domain model that has unnatural links between models can impact a lot how your code looks like. Biggest problem with software development is scaling - which many times is due to other developers not understanding the design of the original developers (because they likely optimized something prematurely :) ). – Stef Jan 22, 2018 at 16:21

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.