title: Infinite Recursion with Jackson JSON and Hibernate JPA issue (yet another)
body: <p>I've found oodles of resources on this, such as this one - <a href="https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue">Infinite Recursion with Jackson JSON and Hibernate JPA issue</a>.  I've <em>tried</em> to implement all of the various suggestions described there (including basic @JsonIgnore), but to no avail.  I can't get anything other than infinite recursion errors regardless of what I try.  I think I have a pretty similar/typical setup, but obviously with something wrong since despite using @JsonManagedReference, @JsonBackReferencere and @JsonIdentityInfo annotations, I keep getting the error.  </p>
  
  <p>My tables are "exchange" and "stock", with a manytoMany between them, and I've been testing by via ExchangeEndpoint.  I've confirmed that if I completely remove "stock" from "exchange" entity, the service works fine, but for some reason, the json annotations do not seem to have any affect.   Below is what I <em>think</em> is the solution based on the second (but more popular) answer in the aforementioned <a href="https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue">Infinite Recursion with Jackson JSON and Hibernate JPA issue</a>.  </p>
  
  <p>Exchange.java</p>
  
  <pre><code>@Entity
  @Table(name = "exchange", schema = "public")
  @XmlRootElement
  @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
  public class Exchange implements java.io.Serializable {
  ...
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "exchange")
      @JsonManagedReference
      public Set&lt;Stock&gt; getStocks() {
          return this.stocks;
      }
  ...
  </code></pre>
  
  <p>Stock.java</p>
  
  <pre><code>@Entity
  @Table(name = "stock", schema = "public")
  @XmlRootElement
  @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
  public class Stock implements java.io.Serializable {
  ...
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "exchangeid", nullable = false)
      @JsonBackReference
      @JsonIgnore
      public Exchange getExchange() {
          return this.exchange;
      }
  ...
  </code></pre>
  
  <p>ExchangeEndpoint.java</p>
  
  <pre><code>@Stateless
  @Path("/exchanges")
  public class ExchangeEndpoint {
      @PersistenceContext(unitName = "postgresql-ss4")
      private EntityManager em;
  ...
      @GET
      @Produces("application/json")
      public List&lt;Exchange&gt; listAll(@QueryParam("start") Integer startPosition,
              @QueryParam("max") Integer maxResult) {
          TypedQuery&lt;Exchange&gt; findAllQuery = em
                  .createQuery(
                          "SELECT DISTINCT e "
                          + "FROM Exchange e "
                          + "LEFT JOIN FETCH e.stocks "
                          + "ORDER BY e.exchangeid",
                          Exchange.class);
          if (startPosition != null) {
              findAllQuery.setFirstResult(startPosition);
          }
          if (maxResult != null) {
              findAllQuery.setMaxResults(maxResult);
          }
          final List&lt;Exchange&gt; results = findAllQuery.getResultList();
          return results;
      }
  </code></pre>
  
  <p>edit:</p>
  
  <p>some of the error output to be sure i'm not misinterpreting something;</p>
  
  <blockquote>
    <p>15:35:16,406 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n]
    (http-/0.0.0.0:8080-1) RESTEASY000100: Failed executing GET
    /exchanges/: org.jboss.resteasy.spi.WriterException:
    org.codehaus.jackson.map.JsonMappingException: Infinite recursion
    (StackOverflowError) (through reference chain:
    net.hb.forge2RestServices.model.Exchange["stocks"]->org.hibernate.collection.internal.PersistentSet[0]->net.hb.forge2RestServices.model.Stock["exchange"]->net.hb.forge2RestServices.model.Exchange["stocks"]->org.hibe
    ...
    ...
    Exchange["stocks"]->org.hibernate.collection.internal.PersistentSet[0]->net.hb.forge2RestServices.model.Stock["exchange"]->net.hb.forge2RestServices.model.Exchange["stocks"])
            at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:262) </p>
  </blockquote>
  
  <p>Please let me know what additional information I can provide to help explain my debacle.  tiy.</p>