【问题标题】:Hibernate @onetomany circular reference error with GsonGson 休眠 @onetomany 循环引用错误
【发布时间】:2016-05-03 05:30:30
【问题描述】:

我尝试了很多方法,我正在详细发布我的问题。

这是我的父类

@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,"projectName":"ABCD","unitDetails":[{"unitDetailsID":575,......

【问题讨论】:

  • 一个丑陋的修复将添加 { 在输出的开头和 } 在输出的末尾
  • 如果是这种情况,我将使用下面的内容,它肯定会发送一个 JSON,但我想知道我在这里缺少什么 GSON String json = gson.toJson(listProject);模型 = json.substring(1, json.length()-1);
  • 如果我只是尝试返回如下模型,那么我会得到层次结构问题 public HttpHeaders index() { model = objProjectDAOImpl.getProjectDetails(userID); return new DefaultHttpHeaders("index").disableCaching(); } net.sf.json.JSONException: 层次结构中有一个循环!
  • Struts ModelDriven的可能重复
  • 我已经详细说明了这个问题的所有内容。所以如果有任何帮助,另一个与它相关但不一样

标签: json hibernate gson struts


【解决方案1】:

这可能不是正确的解决方法,但我不得不接受它。

我设置了父类后,确保子类没有任何对父类的引用或持有任何引用父类的对象,基本上将子类的引用设置为null,这样就不会进去了圆。它对我有用

【讨论】:

    【解决方案2】:

    为什么不使用@JsonIdentityInfo 注释?几个月前我遇到了同样的问题并使用

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

    成功了。 id 属性对应于 YourEntity 中的 getId()。 @JsonIdentityInfo 注释来自 Jackson 库而不是 Gson。作为替代方案,您可以使用同一库中的@JsonManagedReference 和/或@JsonBackReference

    【讨论】:

    • 谢谢,我试试看。
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2015-07-07
    相关资源
    最近更新 更多