我正在尝试为我的 slick 代码生成模型创建一个 Slick 3.1.1 Generic DAO。但是,我面临最后一个无法找到解决方法的编译错误。
整个项目在 GitHub play-authenticate-usage-scala中可用,相关源代码在GenericDao.scala中。
编译器错误如下:
[info] Compiling 16 Scala sources and 1 Java source to /home/bravegag/code/play-authenticate-usage-scala/target/scala-2.11/classes...
[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDao.scala:46: value id is not a member of type parameter ET
[error] def findById(id: PK): Future[Option[ER]] = db.run(tableQuery.filter(_.id === id).result.headOption)
[error] ^
基本上它不识别特征id
下的定义Identifyable
。最重要的声明如下:
trait Identifyable[PK] extends Product {
def id : PK
}
trait GenericDaoHelper {
val profile: slick.driver.JdbcProfile
import profile.api._
class GenericDao[PK, ER <: Identifyable[PK], ET <: Table[ER], TQ <: TableQuery[ET]] @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
(tableQuery: TQ) extends HasDatabaseConfigProvider[JdbcProfile] {
import driver.api._
/**
* Returns the matching entity for the given id
* @param id identifier
* @return the matching entity for the given id
*/
def findById(id: PK): Future[Option[ER]] = db.run(tableQuery.filter(_.id === id).result.headOption)
}
PS:请注意,我正在使用最新的 Slick 3.1.1,这很关键,因为人们过去曾实施过类似的解决方案,但它们在不同版本之间发生了相当大的变化。