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,......