title: Compact mapping one to many in anorm
body: <p>I have two entities.</p>
  
  <p><strong>Good</strong></p>
  
  <pre><code>case class Good(
    var id: Long = null.asInstanceOf[Long],
    var name: String,
    var cost: Money = Moneys.ZERO,
    var category: String,
    var goodComponents: List[GoodComponent] = List.empty
  )
  </code></pre>
  
  <p><strong>GoodComponent</strong></p>
  
  <pre><code>case class GoodComponent(
    var id: Long = null.asInstanceOf[Long],
    var goodId: Long = null.asInstanceOf[Long],
    var product: Product,
    var productAmount: Int
  )
  </code></pre>
  
  <p>Now, I load goods like this:</p>
  
  <pre><code> DB.withConnection(implicit connection =&gt; {
        SQL("""
          select
            goods.*, good_components.*, products.*
          from
            goods
          left join good_components
            on goods.good__id = good_components.good_component__good_id
          left join products
            on good_components.good_component__product_id = products.product__id
          order by
            goods.good__id
        """)
        .as(GoodMapping ~ (GoodComponentMapping ?) *)
        .groupBy(_._1)
        .map({
          case (good: Good, goodComponents: List[(Good ~ Option[GoodComponent])]) =&gt; {
            good.copy(goodComponents = goodComponents.map({case (_ ~ goodComponent) =&gt; { goodComponent }}).flatten)
          }
        })
        .toList
        .sortWith(($1, $2) =&gt; $1.id &lt; $2.id)
      })
  </code></pre>
  
  <p><strong>GoodMapping</strong></p>
  
  <pre><code>  val GoodMapping = {
      get[Long]("good__id") ~
      get[String]("good__name") ~
      get[Int]("good__cost_rubles") ~
      get[Int]("good__cost_kopeks") ~
      get[String]("good__category") map {
        case goodId ~ goodName ~ goodCostRubles ~ goodCostKopeks ~ goodCategory =&gt; Good(
          goodId,
          goodName,
          new Money(
            goodCostRubles,
            goodCostKopeks
          ),
          goodCategory,
          List.empty
        )
      }
    }
  </code></pre>
  
  <p><strong>GoodComponentMapping</strong></p>
  
  <pre><code>  val GoodComponentMapping = {
      get[Long]("good_components.good_component__id") ~
      get[Long]("good_components.good_component__good_id") ~
      get[Long]("good_components.good_component__product_id") ~
      get[Int]("good_components.good_component__product_amount") ~
      get[Long]("products.product__id") ~
      get[String]("products.product__name") ~
      get[Long]("products.product__units") ~
      get[String]("products.product__category") map {
        case goodComponentId ~ goodComponentGoodId ~ goodComponentProductId ~ goodComponentProductAmount ~ productId ~ productName ~ productUnit ~ productCategory =&gt; GoodComponent(
          goodComponentId,
          goodComponentGoodId,
          new Product(
            productId,
            productName,
            ProductUnits.byId(productUnit).getOrElse({
              throw new IllegalArgumentException("Product unit #{" + productUnit + "} not found.")
            }),
            productCategory
          ),
          goodComponentProductAmount
        )
      }
    }
  </code></pre>
  
  <p>I think that need to create on mapping and load goods like</p>
  
  <pre><code>SQL("...").as(GoodMapping *)
  </code></pre>
  
  <p>I want to get advise. Is this better then I do now? How to more elegantly map good (with goodComponents list)?</p>