2016-04-06 15 views
6

Oto moje byty.Nie można utworzyć obiektu zagnieżdżonego z trzech mapowań poziomu w Scali

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) 

Przykład dane:

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) 

Pożądany Wyjście

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

Próbowałem this link ale nie udało się osiągnąć, ponieważ nie mam podmioty referencyjne siebie.

Z góry dziękuję.

+0

Spróbuj tego: http://alvinalexander.com/scala/how-to-create-json-strings-from-scala-objects –

+0

Czy możesz pokazać pełną próbkę kodu? Nie widzę jeszcze żadnej korelacji między twoimi klasami przypadków a kolejnym fragmentem JSON. – Roman

+0

@Roman, zaktualizowałem pytanie o komentarze na temat klas przypadków – Jet

Odpowiedz

0

Jeśli zmienisz swoje klasy do czegoś jak

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 (może być używane poza Play) działa idealnie.