title: Spring MVC: findById - do not include all ManyToOne collections
body: <p>I have 3 entities. ClassA, ClassB and Class C. ClassA have ManyToOne with ClassB and Class C.</p>
  
  <p>If I go from web browser to <code>/classA/1</code> or <code>/classA/abc/1</code> I got very big JSON with ClassA and all items from ClassB and ClassC. </p>
  
  <p>But I want only ClassA and  id of ClassB and id of ClassC.</p>
  
  <p>Where I have an error?</p>
  
  <p>There is simplified code:</p>
  
  <pre><code>/* ------------------------------------------------------ */
  @Entity
  @Table(name="CLASS_A")
  @NamedQuery(name="ClassA.findAll", query="SELECT c FROM ClassA c")
  public class ClassA implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(unique=true, nullable=false)
      private int id;
  
      @ManyToOne
      @JoinColumn(name="class_b_id", nullable=false)
      private ClassB classB;
  
      @ManyToOne
      @JoinColumn(name="class_c_id", nullable=false)
      private ClassC classC;
  
      @Lob
      private String description;
  
      public ClassA() {
      }
      // getters and setters...
  
  }
  /* ------------------------------------------------------ */
  @Entity
  @Table(name = "CLASS_B")
  @NamedQuery(name = "ClassB.findAll", query = "SELECT c FROM ClassB c")
  public class ClassB implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(unique = true, nullable = false)
      private int id;
  
      @Lob
      private String description;
  
      @OneToMany(mappedBy = "classB")
      private List&lt;ClassA&gt; classesA;
  
      public ClassB() {
      }
      // getters and setters...
  }
  /* ------------------------------------------------------ */
  Entity
  @Table(name = "CLASS_C")
  public class ClassC implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(unique = true, nullable = false)
      private int id;
  
      @Lob
      private String description;
  
      @OneToMany(mappedBy = "classC")
      private List&lt;ClassA&gt; classesA;
  
      public ClassC() {
      }
      // getters and setters...
  }
  /* ------------------------------------------------------ */
  @Repository
  public class DaoClassA {    
      @Autowired
      private SessionFactory sessionFactory;
  
      public ClassA findById(int id) {
          return (ClassA) this.sessionFactory.getCurrentSession().
                  get(ClassA.class, id);
      }
  
      public ClassA findByIdMy(int id) {
          return (ClassA) this.sessionFactory.getCurrentSession()
                  .createQuery("SELECT n FROM ClassA n LEFT JOIN FETCH n.classB LEFT JOIN FETCH n.classC WHERE n.id = :id").setInteger("id", id)
                  .uniqueResult();
      }
  }
  /* ------------------------------------------------------ */
  @Controller
  @RequestMapping("/classA")
  public class RestController {
  
      @Autowired
      private ClassAmanager manager;
  
      @RequestMapping(value="/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
      public @ResponseBody ClassA get1(@PathVariable("id") int id) {
          return manager.findById(id);
      }
  
      @RequestMapping(value="/abc/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
      public @ResponseBody ClassA get2(@PathVariable("id") int id) {
          return manager.findByIdMy(id);
      }
  }
  /* ------------------------------------------------------ */
  </code></pre>