Q:

How to JSON a hibernate tree node

@Entity
public class Group{

@Id
@GeneratedValue
private Long id;

@ManyToOne
private Group parent;

@LazyCollection(value=LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "parent")
@Cascade(value = {CascadeType.ALL})
private Set<Group> children = new HashSet<Group>();

}

How do I JSON that structure? Json goes to infinite recursion .. Im using Jackson.
I need to have parent ID in my json output also .

A:

If the Competency entity points to a Group, then you might indeed go into infinite recursion. You can put a @JsonIgnore annotation on the parent and children instance variables to ignore them from being included in the JSON, or you can use a combination of @JsonBackReference/@JsonManagedReference, as the other poster suggested.
Other thing you can do is create a pojo which contains all the properties you are interested in in your service layer, cutting the hibernate connections out of the picture. Something like that:
public class GroupDto {
    private Long id;
    private CompetencyDto parent;
    private List<CompetencyDto> children;
}

public class CompetencyDto {
    private Long id;
}

This, while seeming like overwork, would give you the power your presentation model to not be dependent on your domain model. This would give you much more flexibility when constructing your views:
http://codebetter.com/jpboodhoo/2007/09/27/screen-bound-dto-s/
http://martinfowler.com/eaaDev/PresentationModel.html

