【问题标题】:Converting results of Ebean query to JSON doesn't work将 Ebean 查询的结果转换为 JSON 不起作用
【发布时间】: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


    【解决方案1】:

    终于用thisthis 解决了这个问题。我必须添加

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "from") @JsonBackReference
    public List<Relation> fromThis;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "toArgument") @JsonBackReference
    public List<Relation> toThis;
    

    Argument

    不完全明白为什么,现在我将整个 Argument 对象嵌入到 JSON 中——而不仅仅是我想要的 ID。哦,好吧,我猜。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2011-06-26
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多