本文介绍了SLICK 如何定义双向一对多关系以用于案例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 SLICK 1.0.0-RC2.我定义了以下两个表 Directorate 和 ServiceArea,其中 Directorate 与 ServiceArea 有一对多的关系

I am using SLICK 1.0.0-RC2. I have defined the following two tables Directorate and ServiceArea where Directorate has a one to many relationship with ServiceArea

case class Directorate(dirCode: String, name: String)

object Directorates extends Table[Directorate]("DIRECTORATES") {

  def dirCode = column[String]("DIRECTORATE_CODE", O.PrimaryKey)

  def name = column[String]("NAME")

  def * = dirCode ~ name  <> (Directorate, Directorate.unapply _) 
}

case class ServiceArea(areaCode: String, dirCode: String, name: String)

object ServiceAreas extends Table[ServiceArea]("SERVICE_AREAS") {

  def areaCode = column[String]("AREAE_CODE", O.PrimaryKey)

  def dirCode = column[String]("DIRECTORATE_CODE")

  def name = column[String]("NAME")

  def directorate = foreignKey("DIR_FK", dirCode, Directorates)(_.dirCode)

  def * = areaCode ~ dirCode ~ name <> (ServiceArea, ServiceArea.unapply _)
}

为了使 Directorate 案例类在我的 Play 应用程序表单中有用,我尝试重新定义 Directorate 案例类以具有与该 Directorate 相关的 ServiceAreas 的 Seq.

To make the Directorate case class useful in my Play application form I am trying to redefine the Directorate case class to have a Seq of ServiceAreas that are related to that Directorate.

case class Directorate(dirCode: String, name: String, serviceAreas: Seq[ServiceArea])

我现在的问题是董事会表投影.我试图在董事会中创建一种方法:

My problem is now with the Directorate table projection. I have attempted to create a method in Directorates:

def serviceAreas = (for { a <- ServiceAreas
                         if (a.dirCode === dirCode)
                    } yield (a)).list map {
                      case t: ServiceArea => t
                    }

这样我就可以尝试类似的东西

so that I can try something like

def * = dirCode ~ name  ~ serviceAreas <> (Directorate, Directorate.unapply _)

但这行不通,因为 serviceAreas 只有一种方式.

but this cannot not work as serviceAreas only goes one way.

在我看来,董事会案例类是一个有用的域对象,它应该能够包含相关的服务区域,这似乎是合理的.

It seems reasonable to me that for the Directorate case class to be a useful domain object that it should be able contain the related ServiceAreas.

我想知道我应该如何遍历反向关系,以便 Directorate 表投影起作用.

I'm wondering how I should traverse the inverse relationship so that Directorate table projection will work.

推荐答案

我确信有更优雅的解决方案,但这应该可以解决问题:

I'm sure there is a more elegant solution, but this should do the trick:

import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession

object SlickExperiments2 {

  Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {

    (Directorates.ddl ++ ServiceAreas.ddl).create

    case class Directorate(dirCode: String, name: String) {
      def serviceAreas: Seq[ServiceArea] = (for {
        a <- ServiceAreas
        if (a.dirCode === dirCode)
      } yield (a)).list
    }

    object Directorates extends Table[Directorate]("DIRECTORATES") {

      def dirCode = column[String]("DIRECTORATE_CODE", O.PrimaryKey)

      def name = column[String]("NAME")

      def * = dirCode ~ name <> (Directorate, Directorate.unapply _)
    }

    case class ServiceArea(areaCode: String, dirCode: String, name: String)

    object ServiceAreas extends Table[ServiceArea]("SERVICE_AREAS") {

      def areaCode = column[String]("AREAE_CODE", O.PrimaryKey)

      def dirCode = column[String]("DIRECTORATE_CODE")

      def name = column[String]("NAME")

      def directorate = foreignKey("DIR_FK", dirCode, Directorates)(_.dirCode)

      def * = areaCode ~ dirCode ~ name <> (ServiceArea, ServiceArea.unapply _)
    }

    Directorates.insert(Directorate("Dircode", "Dirname"))

    ServiceAreas.insertAll(ServiceArea("a", "Dircode", "A"), ServiceArea("b", "Dircode", "B"))

    val sa = (for{
    d <- Directorates
    } yield d).list map { case t: Directorate => t.serviceAreas}

    println(sa)
  }                                              
 //> List(List(ServiceArea(a,Dircode,A), ServiceArea(b,Dircode,B)))
}

这篇关于SLICK 如何定义双向一对多关系以用于案例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:47