Failed to create a nested object from three-level mappings in Scala

Here are my essences.

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) 

Sample data:

 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) 

Desired output

 [ { "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": [] } ] } ] 

I tried this link , but this could not be achieved, since I do not have self-learning objects.

Thanks in advance.

+6
source share
1 answer

If you change your classes to something like

 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 (it can be used outside the game) works fine.

0
source

All Articles