无法在Scala中从三个级别映射创建嵌套对象

时间:2016-04-06 10:02:50

标签: java scala recursion

这是我的实体。

case class Entity(id: Long, name: String)

/**
 * entityId is the FK of Entity Table PK
 */
case class Parameter(id: Long, entityId: Long, type: Long, name: String)

//type => 1 = Input, 2 = Output

/**
 * parameterId and sourceParameterId are the FK of Parameter Table PK
 */
case class Source(id: Long, parameterId: Long, sourceParameterId: Long)

示例数据:

Entity(1,"Agriculture")
Entity(2,"Factory")
Entity(3,"Customer")
Entity(4,"Institute")
Entity(5,"Student")

Parameter(1,1,2,"Raw Food")
Parameter(2,2,1,"Raw Food")
Parameter(3,2,2,"Packed Food")
Parameter(4,3,1,"Packed Food")
Parameter(5,4,2,"Knowledge")
Parameter(6,5,1,"Knowledge")

Source(1,2,1)
Source(2,4,3)
Source(3,6,5)

期望输出

[
    {
        "id": 1,
        "name": "Agriculture",
        "item": [
            {
                "id": 2,
                "name": "Factory",
                "item": [
                    {
                        "id": 3,
                        "name": "Customer",
                        "item": []
                    }
                ]
            }
        ]
    },
    {
        "id": 4,
        "name": "Institute",
        "item": [
            {
                "id": 5,
                "name": "Student",
                "item": []
            }
        ]
    }
]

我尝试this link但未能实现,因为我没有自我引用实体。

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您将课程更改为

case class Entity(id: Long, name: String)
case class Parameter(id: Long, entity: Entity, type: Long, name: String)
case class Source(id: Long, parameter: Parameter, sourceParameterId: Long)

play-json(它可以在Play之外使用)完美无缺。