我尝试了很多方法,并且发布了详细的问题。休眠@onetomany与Gson的循环引用错误
这里是我的父类
@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;
}
这里是我的子类
@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;
}
这里是我的控制器
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();
}
我能正确保存的细节没有任何循环错误。当我尝试检索时,我收到循环引用错误。然后我用GSON仅暴露出必填字段,现在我得到以下
HTTP Status 500 - A JSONObject text must begin with '{' at character 1 of
错误我明白,这是不是一个JSON格式,但我认为GSON会照顾这还是让我知道我必须使用以不同的方式来解决这个
它显示的结果如下,其看起来像一个阵列 [{ “ID”:139, “项目名”: “ABCD”, “unitDetails”:[{ “unitDetailsID”:575, ......
一个丑陋的修复将被添加{在输出的开始和}在输出的结尾 – Satya
如果是这样的话,我会去下面这肯定会发送一个JSON,但我想知道我是什么在这里与GSON错过 字符串json = gson.toJson(listProject); model = json.substring(1,json.length() - 1); – CrazyMac
如果我只是试图返回模型如下,然后我得到的层次结构问题 \t公共HttpHeaders索引(){ \t \t model = objProjectDAOImpl.getProjectDetails(userID); \t \t return new DefaultHttpHeaders(“index”)。disableCaching(); \t} net.sf.json.JSONException:层次结构中存在一个循环! – CrazyMac