【发布时间】:2014-04-04 17:39:41
【问题描述】:
我现在有
@Entity
public class Argument extends Model
{
@Id
public Long id;
@Required @NotEmpty @Size(max = 140)
public String summary;
@SuppressWarnings("unchecked")
public static Finder<Long, Argument> find = new Finder(Long.class, Argument.class);
...
}
和
@Entity
public class Relation extends Model
{
@Id
public Long id;
@Required @ManyToOne @NotNull @JsonManagedReference
public Argument from;
@ManyToOne @JsonManagedReference
public Argument toArgument;
@ManyToOne @JsonManagedReference
public Relation toRelation;
@Required @NotNull
public Integer type;
...
}
基本上,Relation 将两个参数(或一个参数和另一个关系)链接在一起。这是两个类之间的单向关系。然而我得到了
[RuntimeException: java.lang.IllegalArgumentException: Infinite recursion
(StackOverflowError) (through reference chain: models.Argument["relations"]->
com.avaje.ebean.common.BeanList[0]->models.Relation["from"]->
models.Argument["relations"]-> ... ->models.Argument["relations"])
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:611) ~[jackson-databind.jar:2.2.2]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142) ~[jackson-databind.jar:2.2.2]
...
当我尝试做时
find.where().or(Expr.eq("from", argument), Expr.eq("toArgument", argument)).findList();
然后在该结果上调用Json.toJson。
将@JsonBackReference 添加到引用Argument 的字段可以通过完全省略这些字段来解决问题,这不是我想要的。添加@JsonManagedReference 使问题保持不变,因为我看不到添加@JsonBackReference 的任何地方——这是一个单向关系,该死!
基本上,我想要的只是一个涉及某个参数的 JSON 关系数组。表示关系的 JSON 对象应该只包含 ID——像 {id: 1, from: 4, toArgument: 3, type: 1} 这样的东西正是我想要的。
编辑我应该补充一点,以前在 Argument 模型中运行良好的视图现在在请求使用 JSON 格式时遇到了同样的错误——尽管 只能运行有论据!当我根本不更改 Argument 时,为什么完全不同的类中的某些东西会影响 Argument?
【问题讨论】:
标签: playframework playframework-2.0 ebean