Wednesday 15 February 2017

    Creating Circular referencing in Hibernate



There are situations when we need to create tree json. For that we need to create self join on the same Entity to make Parent child relationship on same entity.

Following is the  example:

            @Entity
            @Table(name = "tree")
             public class Tree implements Serializable {

                  private static final long serialVersionUID = 663408095532480033L;

                  @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
                  @Column(name="id")
                   private String id;

                  @Column(name="fruit")
                  private String fruit ;

                 @ManyToOne
                 @JoinColumn(name="child_id", nullable=false)
                  private Tree rootId;

}

When you will try to retrieve the following entity you will get Child First and inside it will get parents.

In order to retrieve parent first and then inside get childs following should be the structure.


            @Entity
            @Table(name = "tree")
             public class Tree implements Serializable {

                  private static final long serialVersionUID = 663408095532480033L;

                  @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
                  @Column(name="id")
                   private String id;

                  @Column(name="fruit")
                  private String fruit ;

                 @ManyToOne
                 @JoinColumn(name="child_id", nullable=false)
                  private Tree rootId;

                 @OneToMany(mappedBy=rootId)
                 private List<Tree> children;

           }

Here when you will return the entity to frontend user issue of infinite json refrencing will come in order to resolve the issue need to add json annotations in entity that is following :

            @Entity
            @Table(name = "tree")
             public class Tree implements Serializable {

                  private static final long serialVersionUID = 663408095532480033L;

                  @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
                  @Column(name="id")
                   private String id;

                  @Column(name="fruit")
                  private String fruit ;

                 @ManyToOne
                 @JoinColumn(name="child_id", nullable=false)
                 @JsonBackReference
                  private Tree rootId;

                 @OneToMany(mappedBy=rootId)
                 @JsonManagedReference
                 private List<Tree> children;

           }

Enitity would return the  following Json:

[
  {
    "id": "1",
    "fruit": "ROOT",
    "child": [
      {
        "id": "2",
        "fruit": "CHILD",
        "child": [
          {
            "id": "3",
            "fruit": "CHILD1",
            "child": []
          }
        ]
      }
    ]
  },
  {
    "id": "4",
    "fruit": "Root",
    "child": [
      {
        "id": "5,
        "fruit": " Root Child 1",
        "child": [
          {
            "id": "6",
            "fruit": " Root Child 2"
            "child": []
          }
        ]
      }
]

Hope this will help happycoding. :)