title: Hibernate Self join query wont parse to json
body: <p>I am using Hibernate to persist the data into a HSQLDB file.<br>
  I'd created a method similar to a tester and it "populates" and "query" the data that i'd stored.</p>
  
  <p>But when i query the data, in debug mode it's like an infinite array something like:</p>
  
  <p>Product (instance1 of product)<br>
     -> ParentProduct (instance2 of product)<br>
        -> ChildProduct (this guy is the same as instance 1)<br>
  And when i try to retrieve this as Json it breaks with a huge error:</p>
  
  <pre><code>com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:618)
      com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:401)
      com.google.gson.stream.JsonWriter.value(JsonWriter.java:527)
      com.google.gson.internal.bind.TypeAdapters$11.write(TypeAdapters.java:310)
      com.google.gson.internal.bind.TypeAdapters$11.write(TypeAdapters.java:295)
      com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
      com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
      com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
      com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
  </code></pre>
  
  <p>Here's the source:</p>
  
  <pre><code>package com.thalesgomes.ws.rest.classes;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.*;
  
  @Entity
  @Table(name="PRODUCT")
  public class Product {
  
      @Id
      @GeneratedValue
      @Column(name="product_id")
      private Long id;
  
      @ManyToOne(cascade= {CascadeType.ALL})
      @JoinColumn(name="parent_id")
      private Product parent;
  
      private String name;
  
      private String description;
  
      @OneToMany(mappedBy="parent",cascade={CascadeType.ALL})
      private List&lt;Product&gt; children;
  
      @OneToMany(mappedBy="product",cascade={CascadeType.ALL})
      private List&lt;Image&gt; images;
  
  
  
      //Getters and Setters
  
  }
  </code></pre>
  
  <p>And here:    </p>
  
  <pre><code>package com.thalesgomes.ws.rest.JPA;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  
  import com.google.gson.Gson;
  import com.thalesgomes.ws.rest.classes.Image;
  import com.thalesgomes.ws.rest.classes.Product;
  
  public class ProductJPA {
  
      public static void main(String[] args) {
          populateDatabase();
          getProducts();
  
      }
  
      private static void getProducts() {
          // TODO Auto-generated method stub
          EntityManager em = getManager();
          em.getTransaction().begin();
          List&lt;Product&gt; products  = em.createQuery("from Product", Product.class).getResultList();
          Gson gson = new Gson();
          String json = gson.toJson(products);
          System.out.println(json);
      }
  
      public static EntityManager getManager() {
          EntityManagerFactory factory = Persistence.createEntityManagerFactory("Persistence");
          return factory.createEntityManager();
      }
  
      public static void populateDatabase() {
  
          List&lt;Image&gt; images = new ArrayList&lt;Image&gt;();
          Image img1 = new Image();
          Image img2 = new Image();
  
          Product parent = new Product();
          parent.setDescription("descriptionParent");
          parent.setName("nameParent");
          parent.setParent(null);
  
          Product child1 = new Product();
  
  
          img1.setProduct(parent);
          img1.setType("jpeg");
          images.add(img1);
  
          child1.setName("name1");
          child1.setDescription("description1");
          child1.setParent(parent);
          child1.setImages(images);
  
          Product child2 = new Product();
          img2.setProduct(child2);
          img2.setType("jpeg");
          images.add(img2);
          child2.setName("name2");
          child2.setDescription("description2");
          child2.setParent(parent);
          child2.setImages(images);
  
  
  
  
  
          EntityManager manager = getManager();
          manager.getTransaction().begin();
          try {
              manager.persist(img1);
              manager.persist(img2);
              manager.persist(parent);
              manager.persist(child1);
              manager.persist(child2);
  
          } catch (Exception e) {
              manager.getTransaction().rollback();
          }
          manager.getTransaction().commit();
          System.out.println("Commit done.");
          manager.close();
  
      }
  
  }
  </code></pre>