目前我有这样的事情:
val q = for {
department <- departments if department.id === x
employee <- employees if employee.departmentId === department.id
} yield (department, employee)
这会给我:
(sales, john)
(sales, bob)
(finance, william)
(finance, helen)
然后我按部门对结果进行分组:
val grouped = results.groupBy(_._1).mapValues(_.map(_._2))
给我:
(sales -> (john, bob))
(finance -> (wiliam, helen)
我想避免元组。虽然在这个简单的例子中很清楚,但如果我想要以结构化格式显示部门、经理、副手和员工列表,它很快就会变得难以管理。如果查询和结果处理在源代码中彼此不接近,则尤其如此。
我怎样才能在查询中产生除元组之外的其他内容?
我试图产生一个案例类:
case class DeptEmployeeRow(department: Department, employee: Employee)
val q = for {
department <- departments if department.id === x
employee <- employee if employee.id
} yield DeptEmployeeRow(department, employee)
但光滑不喜欢这样。使用单态案例类和 slick 的 CaseClassShape 不起作用,因为它只支持内置类型,即我可以使用:
yield DeptEmployeeRow(department.name, employee.name)
but not
yield DeptEmployeeRow(department, employee)