2016-01-14 61 views
0

做我的第一個步驟,用油滑,我有這個表光滑模式 - 獲取連接表

case class Employee(name: String,last: String,department: Option[Int] = None,id: Option[Int] = None) 
class Employees (tag: Tag) extends Table[Employee](tag, "EMPLOYEES") { 
    // Auto Increment the id primary key column 
    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("NAME", O.NotNull) 
    def last = column[String]("LAST", O.NotNull) 
    def dept = foreignKey("EMP_FK",deptId,departments)(_.id) 
    def * = (name,last,deptId.?, id.?) <> (Employee.tupled, Employee.unapply) 
    val departments = TableQuery[Departments] 
} 
case class DepartmentManager(id:Int,name:String,manager:String) 
case class Department(id:Option[Int],name:String,managerId:Int) 
class Departments (tag: Tag) extends Table[Department](tag, "DEPARTMENTS") { 
    val employees = TableQuery[Employees] 
    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("NAME", O.NotNull) 
    def managerId = column[Int]("MANAGER_ID", O.Nullable) 
    def manager = foreignKey("EMP_FK",managerId,employees)(_.id) 
    def * = (id.?,name,managerId) <> (Department.tupled, Department.unapply) 
} 

,但我得到的編譯錯誤:

類型的查詢表達式[沒什麼,沒什麼, SEQ]不符合 預計類型列表[DepartmentManager

我試圖做這樣的事情(只是爲了檢查,我知道這是不好的):

def all: List[DepartmentManager] = db.withSession { implicit session => 
    val employees = TableQuery[Employees] 
    val x = for { 
     (d, e) <- departments join employees 
    } yield (d.id, d.name, e.name + " " + e.last) 

    x.iterator.map(t=> DepartmentManager(t._1,t._2,t._3)).toList 
    } 

但它並沒有給我想要的結果 - 後者(t._1,t._2,t._3)的結果看起來像

(1,富,(員工路徑@ 1076478352._2).NAME(僱員路徑 @ 1076478352._2).LAST)

請指教

回答

0
case class Employee(name: String, last: String, department: Option[Int] = None, id: Option[Int] = None) 

class Employees (tag: Tag) extends Table[Employee](tag, "EMPLOYEES") { 
    // Auto Increment the id primary key column 
    val departments = TableQuery[Departments] 

    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("NAME", O.NotNull) 
    def last = column[String]("LAST", O.NotNull) 
    def deptId = column[Int]("DEPARTMENT", O.NotNull) 

    def deptFK = foreignKey("EMP_DEP_FK", deptId, departments)(_.id) 

    def * = (name, last, deptId.?, id.?) <>(Employee.tupled, Employee.unapply) 
} 

case class DepartmentManager(id: Int, name: String, manager: String) 

case class Department(id: Option[Int], name: String, managerId: Int) 

class Departments (tag: Tag) extends Table[Department](tag, "DEPARTMENTS") { 
    val employees = TableQuery[Employees] 
    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("NAME", O.NotNull) 
    def managerId = column[Int]("MANAGER_ID", O.Nullable) 

    def managerFK = foreignKey("DEP_MAN_FK", managerId, employees)(_.id) 

    def * = (id.?, name, managerId) <>(Department.tupled, Department.unapply) 
} 


def all(implicit s: Session): List[DepartmentManager] = { 
    val employees = TableQuery[Employees] 
    val x = for { 
     (d, e) <- departments join employees on (_.managerId === _.id) 
    } yield (d.id, d.name, e.name + " " + e.last) 

    x.list.map(t => DepartmentManager(t._1, t._2, t._3)) 
} 

呦您需要隱含的Session對象在查詢上調用listiterator方法。

+0

謝謝,我有我的編輯與會議的問題,但經理姓名的結果看起來是這樣的: \t(僱員路徑@ 1076478352._2).NAME(僱員路徑@ 1076478352._2).LAST – igx