Getting duplicate items when querying a collection with Spring Data Rest

This is causing your issue (Page Entity):

  public void setModule(Module module) {
    this.module = module;
    this.module.addPage(this); //this line right here
  }

Hibernate uses your setters to initialize the entity because you put the JPA annotations on getters.

Initialization sequence that causes the issue:

  1. Module object created
  2. Set Module properties (pages set is initialized)
  3. Page object created
  4. Add the created Page to Module.pages
  5. Set Page properties
  6. setModule is called on the Page object and this adds (addPage) the current Page to Module.pages the second time

You can put the JPA annotations on the fields and it will work, because setters won't be called during initialization (bonus question).