2016-05-03 25 views
1

我尝试了很多方法,并且发布了详细的问题。休眠@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, ......

+0

一个丑陋的修复将被添加{在输出的开始和}在输出的结尾 – Satya

+0

如果是这样的话,我会去下面这肯定会发送一个JSON,但我想知道我是什么在这里与GSON错过 字符串json = gson.toJson(listProject); model = json.substring(1,json.length() - 1); – CrazyMac

+0

如果我只是试图返回模型如下,然后我得到的层次结构问题 \t公共HttpHeaders索引(){ \t \t model = objProjectDAOImpl.getProjectDetails(userID); \t \t return new DefaultHttpHeaders(“index”)。disableCaching(); \t} net.sf.json.JSONException:层次结构中存在一个循环! – CrazyMac

回答

0

这可能不是正确的修复方法,但我不得不使用它。

设置父类后,我确保子类没有任何引用父类或持有引用父类的任何对象,基本上将子引用设置为null,以便它不去在通告中。它对我有用

0

为什么你不使用@JsonIdentityInfo注释?我在几个月前曾经发过同样的问题,并且使用了

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

诀窍。 id属性对应于YourEntity中的getId()。 @JsonIdentityInfo注释来自杰克逊图书馆而非Gson。作为替代,您可以使用来自同一个库的@JsonManagedReference和/或@JsonBackReference

+0

谢谢,我会试一试。 – CrazyMac