如何在这个巧妙的通用 DAO 特征中通过类型参数替换 id 类型?
How to replace id type by type parameter in this slick generic DAO trait?
如何用类型参数 ID
替换具体类型 Long
?
trait GenericDaoProfile[T <: HasId[Long]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[Long]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[Long, T] {
val query: TableQuery[TB]
override def store(t: T): Option[Long] = {
assert(t.id.isEmpty)
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
我试过:
trait GenericDaoProfile[ID, T <: HasId[ID]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[ID]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[ID, T] {
val query: TableQuery[TB]
override def store(t: T): Option[ID] = {
assert(t.id.isEmpty)
// error for code below:
// Error:(42, 61) No matching Shape found.
// Slick does not know how to map the given types.
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
似乎 slick 不知道如何映射任意类型 ID
但基元。有没有办法告诉 scala 编译器 ID
将是一个
原始类型?
override def store(t: T)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[ID], ID, Rep[ID]])
您可能需要提供这样的形状
如何用类型参数 ID
替换具体类型 Long
?
trait GenericDaoProfile[T <: HasId[Long]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[Long]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[Long, T] {
val query: TableQuery[TB]
override def store(t: T): Option[Long] = {
assert(t.id.isEmpty)
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
我试过:
trait GenericDaoProfile[ID, T <: HasId[ID]] {
this: JdbcProfile =>
import driver.api._
trait HasIdColumn {
def id: Rep[ID]
}
trait GenericDao[TB <: Table[T] with HasIdColumn] extends Dao[ID, T] {
val query: TableQuery[TB]
override def store(t: T): Option[ID] = {
assert(t.id.isEmpty)
// error for code below:
// Error:(42, 61) No matching Shape found.
// Slick does not know how to map the given types.
Some(Await.result(DB.db.run((query returning query.map(_.id)) += t), Duration.Inf))
}
}
}
似乎 slick 不知道如何映射任意类型 ID
但基元。有没有办法告诉 scala 编译器 ID
将是一个
原始类型?
override def store(t: T)
(implicit shape: Shape[_ <: FlatShapeLevel, Rep[ID], ID, Rep[ID]])
您可能需要提供这样的形状