在ArrayList中链接两个ArangoDB集合时反序列化问题

时间:2016-12-26 13:02:19

标签: java arangodb json-deserialization

我有两个数据库集合Agency & Program,其中Agency可以有许多programs,所有进一步的概念实现仅使用程序。所以我创建了两个POJO

public class Agency implements Serializable {
    @DocumentField(DocumentField.Type.ID)
    private String agencyId;

    @DocumentField(DocumentField.Type.KEY)
    @SerializedName("AGENCYNAME")
    private String agencyName;

    @SerializedName("SHORTNAME")
    private String shortName;

    @Expose(serialize = false, deserialize = true)
    @SerializedName("PROGRAMS")
    private List<Program> programs;

    // Other fields with Getter & Setters
}


public class Program implements Serializable {

    @DocumentField(DocumentField.Type.ID)
    private String programId;

    @SerializedName("PROGRAMNAME")
    private String programName;

    @DocumentField(DocumentField.Type.KEY)
    @SerializedName("SHORTNAME")
    private String shortName;

    @SerializedName("AGENCY")
    private Agency agency;

    // Other fields with Getter & Setters
}

当我运行AQL : for a in AGENCY return merge(a, {PROGRAMS: (for p in PROGRAMS FILTER p.AGENCY == a._id return p)})

我得到了以下JSON

[
  {
    "AGENCYNAME": "Dummy Agency 1",
    "SHORTNAME": "DA1",
    "_id": "AGENCY/1062620",
    "_key": "1062620",
    "_rev": "_URnzj-C---",
    "PROGRAMS": [
      {
        "_key": "DA1DP1",
        "_id": "PROGRAMS/DA1DP1",
        "_rev": "_UQ6dGOG---",
        "AGENCY": "AGENCY/1062620",
        "PROGRAMNAME": "DA1 Dummy Program 1",
        "SHORTNAME": "DA1DP1"
      }
    ]
  },
  {
    "AGENCYNAME": "Dummy Agency 2",
    "SHORTNAME": "DA2",
    "_id": "AGENCY/1062358",
    "_key": "1062358",
    "_rev": "_URnzj-C---",
    "PROGRAMS": [
      {
        "_key": "DA2DP1",
        "_id": "PROGRAMS/DA2DP1",
        "_rev": "_UQ6dGOG---",
        "AGENCY": "AGENCY/1062358",
        "PROGRAMNAME": "DA2 Dummy Program 1",
        "SHORTNAME": "DA2DP1"
      }
    ]
  }
]

当我从arangodb-java-driver 4.1运行此查询时,它会在反序列化时抛出异常 com.arangodb.velocypack.exception.VPackValueTypeException: Expecting type OBJECT

如果我评论来自Agency.java的这些行,它可以正常工作,但我需要在代理中使用List。

    @Expose(serialize = false, deserialize = true)
    @SerializedName("PROGRAMS")
    private List<Program> programs;

在这种情况下有办法处理列表吗? 请建议一种方法来克服这个.. 谢谢:))

1 个答案:

答案 0 :(得分:1)

在您的POJO Program中,您有一个来自Agency类型的字段代理商,但在您从数据库获取的JSON中,此字段来自类型字符串。将类型Agency更改为String,您的代码就可以使用。

<强>更新

要从您的查询中获得适合当前java bean的结果,请运行:

for a in AGENCY return merge(a, {PROGRAMS: (for p in PROGRAMS FILTER p.AGENCY == a._id return merge(p, {AGENCY: a}))})