Scala Skinny ORM - 强制性关系

时间:2015-10-09 05:47:42

标签: scala orm

属于强制关系

我不想将typeWine定义为可选值,但如果我不这样做,我必须在提取方法中声明typeWine并且我不知道如何做

在Skinny ORM的文档中,它没有描述如何做到这一点,而且我被卡住了。

package app.models.wine

import scalikejdbc._
import skinny.orm.SkinnyCRUDMapper

case class Wine (id: Option[Long], typeWine: Option[Type] = None, name: String)

object Wine extends SkinnyCRUDMapper[Wine] {  
  override def defaultAlias = createAlias("w")
  override def extract (rs: WrappedResultSet, n: ResultName[Wine]): Wine = new Wine(
    id = rs.get(n.id),
    name = rs.get(n.name)
  )
  belongsTo[Type](Type, (w, t) => w.copy(typeWine = t)).byDefault
}  


package app.models.wine

import scalikejdbc._
import skinny.orm.SkinnyCRUDMapper

case class Type (id: Option[Long], typeName: String)

object Type extends SkinnyCRUDMapper[Type] {
  override def defaultAlias = createAlias("t")
  override def columnNames = Seq("id", "type_name")
  override def extract (rs: WrappedResultSet, n: ResultName[Type]): Type = new Type(
    id = rs.get(n.id),
    typeName = rs.get(n.typeName)
  )
}

1 个答案:

答案 0 :(得分:0)

将belongsTo / byDefault关系定义为typeWine,并使用resultName提取typeWine值应该适合您。