title: Using Spring Data JPA - how to save ONLY UNIQUE items in child entity while saving parent entity and using CascadeType=PERSIST
body: <p>I am preparing simple Spring app. I have 2 entities :
  Book.class (parent) and Author.class (child): with @OneToMany from Author view and @ManyToOne(cascade=CascadeType.PERSIST) from Book view relations. While saving new Book - also Author is being saved and added to DB( mySql)- which is what I want. But I cannot understand why Spring adds Author - if such item already exists. How to change the code to make sure that <strong>only unique</strong> Authors will be added to DB and there will be <strong>no duplicates</strong> in author table <strong>in DB</strong>?</p>
  
  <p>I've added hashCode and equals methods to Author class but it did not help.
  I've tried to change also Cascade.Type but also did not help.</p>
  
  <p>The Author.class(part of code):</p>
  
  <pre><code>@Entity
  public class Author {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String firstName;
  private String lastName;
  @OneToMany(mappedBy = "author")
  @JsonManagedReference(value = "book-author")
  private Set&lt;Book&gt; books = new HashSet&lt;Book&gt;();
  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      Author author = (Author) o;
      return Objects.equals(getFirstName(), author.getFirstName()) &amp;&amp;
              Objects.equals(getLastName(), author.getLastName());
      }
  
      @Override
      public int hashCode() {
      return Objects.hash(getFirstName(), getLastName());
      }
  </code></pre>
  
  <p>And the Book.class(part of code):</p>
  
  <pre><code>@Entity
  public class Book {
  @Id
  @GeneratedValue(strategy= GenerationType.IDENTITY)
  private Long id;
  private String title;
  
  @ManyToOne( cascade=CascadeType.PERSIST)
  @JoinColumn(name = "author_id", unique = true)
  @JsonBackReference(value="book-author")
  private Author author;
  </code></pre>
  
  <p><strong>Edit 1</strong>
  BookServiceImpl.class</p>
  
  <pre><code>@Override
   public BookDto addBookDto(BookDto bookDto) {
      Book book = bookConverter.apply(bookDto);
      bookRepository.save(book);
      return bookDtoConverter.apply(book);
  }
  </code></pre>
  
  <p>AuthorServiceImpl.class</p>
  
  <pre><code>@Override
   public Author findAuthor(String firstName, String lastName) {
      Optional&lt;Author&gt; authorByNameOptional =      authorRepository.findByFirstNameAndLastName(firstName, lastName);
      if (authorByNameOptional.isPresent()) {
  
          return authorByNameOptional.get();
      } else {
          Author newAuthor = new Author();
          newAuthor.setFirstName(firstName);
          newAuthor.setLastName(lastName);
          return newAuthor;
      }
  </code></pre>
  
  <p>And BookWebController.class</p>
  
  <pre><code>@PostMapping("/addWebBook")
      public String addBook(@ModelAttribute(name = "addedBook") BookDto addedBook, Model model) {
          Author author1 = addedBook.getAuthor();
          Author author = authorService.findAuthor(author1.getFirstName(), author1.getLastName());
          addedBook.setAuthor(author);
          bookService.addBookDto(addedBook);
          return "redirect:/message?msg";
      }
  </code></pre>
  
  <p>Would be greatful for any hint as I am quite new to this area :-)</p>