title: Spring Data JPA and Jackson JsonIdentity weirdness with one-to-many relationships
body: <p>I'm observing some weird jackson behavior which I need help with. I have the following two one-to-many relationships in my simple library application:</p>
  
  <ul>
  <li><code>Book</code> contains a list of <code>BookCopy</code> </li>
  <li><code>Library</code> contains a list of <code>BookCopy</code></li>
  </ul>
  
  <p>The idea is that a single book may have many copies, and each library owns copies of books (and not books directly). That said, here's the relevant snippet of my code:</p>
  
  <p><code>Book</code> class:</p>
  
  <pre><code>@Entity
  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  public class Book {
      //.. rest omitted
      @OneToMany(mappedBy = "book")
      private List&lt;BookCopy&gt; bookCopies;
  }
  </code></pre>
  
  <p><code>Library</code> class:</p>
  
  <pre><code>@Entity
  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  public class Library {
  @OneToMany(mappedBy = "library")
  private List&lt;BookCopy&gt; bookCopies;
  }
  </code></pre>
  
  <p><code>BookCopy</code> class:</p>
  
  <pre><code>@Entity
  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  public class BookCopy {
  @ManyToOne
  @JoinColumn(name = "book_id")
  private Book book;
  
  @ManyToOne
  @JoinColumn(name = "library_id")
  private Library library;
  }
  </code></pre>
  
  <p>Now, when I add a book and a library, without the copies, things work fine. When I add a book-copy object though, I get some seriously strange output from the serialized version of <code>bookRepository.findAll()</code>. </p>
  
  <p>This is a sample json I'm POST-ing to add a book copy:</p>
  
  <pre><code>{
    "book": {
    "id": 1
    },
   "library": {
    "id": 4
   }
  </code></pre>
  
  <p>}</p>
  
  <p>.. and things are persisted perfectly into the DB (postgres). However, when I retrieve all books, here's a sample weird JSON I get (snipped for brevity):</p>
  
  <pre><code>[
    {
      "id": 1,
      "title": "Effective Java",
      "bookCopies": [
        {
          "id": 5,
          "book": 1,
          "library": {
            "id": 3 // ...
          }
        }
    },
    2
  ]
  </code></pre>
  
  <p>Note the last part - the weird <code>2</code> sticks out of nowhere - in a list of book objects. Am I doing something seriously wrong - or is this some jackson bug I'm hitting?</p>