1

I tried many ways and I am posting my issue in detail.

Here is my parent class

@Entity
@Table(name = "Project")
//@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Project implements Serializable{
@Expose
int id;
@Expose
String projectName;
@Expose
String userID;
// Date dateCreated;
@Expose
String dateCreated;
@Expose
String status;
@Expose
String houseType;

@Expose
private List<UnitDetails> unitDetails = new ArrayList<>();
@Expose
private List<RoomDetails> roomDetails  = new ArrayList<>();
    @GeneratedValue
@Column(name = "id")
public int getId() {
    return id;
}
    @OneToMany( mappedBy="unitDetails", fetch = FetchType.LAZY)
public List<UnitDetails> getUnitDetails() {
    return unitDetails;
}

public void setUnitDetails(List<UnitDetails> unitDetails) {
    this.unitDetails = unitDetails;
}

@OneToMany(mappedBy="roomDetails", fetch = FetchType.LAZY)
public List<RoomDetails> getRoomDetails() {
    return roomDetails;
}

public void setRoomDetails(List<RoomDetails> roomDetails) {
    this.roomDetails = roomDetails;
}

Here is my child classes

@Entity
@Table(name="Unit_Details")
//@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class UnitDetails {
@Expose
int unitDetailsID;
@Expose
int designID;
@Expose
String unit;
@Expose
double length;
@Expose
double breadth;
@Expose
double height;
@Expose
String img;
@Expose
String type;
@Expose
String color;

@JsonIgnore
private Project unitDeTails;

@Id
@GeneratedValue
@Column(name="unitDetailsID", unique=true, nullable=false)
public int getUnitDetailsID() {
    return unitDetailsID;
}
    @ManyToOne  
@JoinColumn(name="id",  insertable=true, updatable=false)
public Project getUnitDetails() {
    return unitDeTails;
}

public void setUnitDetails(Project unitDetails) {
    this.unitDeTails = unitDetails;
}

Here is my controller

public class ProjectController extends ActionSupport implements
    ModelDriven<Object> {
    public HttpHeaders index() {

    model = objProjectDAOImpl.getProjectDetails(userID);

    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    System.out.println(gson.toJson(model));
    model = gson.toJson(model);
    return new DefaultHttpHeaders("index").disableCaching();
}

I was able to save the details properly without any cyclic error. When I try to retrieve, I received the cyclic reference error. Then I used Gson to expose only the required fields and now I get the error below

HTTP Status 500 - A JSONObject text must begin with '{' at character 1 of 

I understand that this is not a JSON format but I thought GSON will take care of this or let me know I have to use a different way to fix this

It displays the result as below which looks like an array [{"id":139,"projectName":"ABCD","unitDetails":[{"unitDetailsID":575,......

Roman C
  • 49,761
  • 33
  • 66
  • 176
CrazyMac
  • 462
  • 4
  • 19
  • an ugly fix will be add { at the begining of output and } at the end of output – Satya May 03 '16 at 05:34
  • If that is the case, I will go with the below which will definitely send a JSON but I would like to know what am I missing here with GSON String json = gson.toJson(listProject); model = json.substring(1, json.length()-1); – CrazyMac May 03 '16 at 05:45
  • If I just try to return the model as below, then I get the hierarchy issue public HttpHeaders index() { model = objProjectDAOImpl.getProjectDetails(userID); return new DefaultHttpHeaders("index").disableCaching(); } net.sf.json.JSONException: There is a cycle in the hierarchy! – CrazyMac May 03 '16 at 06:02
  • 1
    Possible duplicate of [Struts ModelDriven](http://stackoverflow.com/questions/36871821/struts-modeldriven) – Aleksandr M May 03 '16 at 08:58
  • I have detailed everything in this question.. so if there is any help, the other one is related to it but not the same – CrazyMac May 03 '16 at 09:39

2 Answers2

0

This may not be the correct fix but I had to go with it.

After I set the parent class, I ensured the child class doesn't have any reference to the parent or holding any object referring to the parent class, basically set the child reference to null, so that it doesn't go in circular. It worked for me

CrazyMac
  • 462
  • 4
  • 19
0

Why you don't use the @JsonIdentityInfo annotation? I had the same issue months ago and using

@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class, scope = YourEntity.class)

did the trick. The property id corresponds to getId() in YourEntity. The @JsonIdentityInfo annotation is from the Jackson library not Gson. As an alternative you can use @JsonManagedReference and/or @JsonBackReference from the same lib.

Sal
  • 1,230
  • 13
  • 21