【发布时间】:2017-04-21 02:36:07
【问题描述】:
请耐心等待,在 OP 有意义之前有一些上下文。我正在使用 Slick 3.1.x 和 slick 代码生成器。顺便说一句,整个源代码可以在play-authenticate-usage-scala github project 中找到。对于这个项目,我希望有一个灵活的通用 Dao,以避免为每个模型重复相同的样板代码。
我有一个 postgres sql 脚本,它在这里使用进化创建数据库: 1.sql
然后我调用生成以下数据模型的生成器: Tables.scala
为了能够为模型类提供通用的 dao slick 实现,我需要它们遵守一些基本抽象,例如
-
Entity 特征:每个实体都有一个
id,例如需要dao的findById -
AutoIncEntity trait 声明了方法
def copyWithNewId(id : PK) : Entity[PK]。这对于 dao 的createAndFetch实现是必需的,它持久化一个新实体并在一步中检索自动生成的idPK。
这个copyWithNewId 是 OP 的重点。请注意,它被称为copyWithNewId 而不是copy 以避免无限递归。为了能够实现允许插入和立即获取自动生成的id 的GenericDaoAutoIncImpl,实体行需要来自<Model>Row 案例类的copy(id = id) 方法,该方法在定义GenericDaoAutoIncImpl 时目前还不知道。相关实现如下:
override def createAndFetch(entity: E): Future[Option[E]] = {
val insertQuery = tableQuery returning tableQuery.map(_.id)
into ((row, id) => row.copyWithNewId(id))
db.run((insertQuery += entity).flatMap(row => findById(row.id)))
}
这需要我在每个 AutoInc id 生成的模型中实现 copyWithNewId 方法,这并不好,例如
// generated code and modified later to adapt it for the generic dao
case class UserRow(id: Long, ...) extends AutoIncEntity[Long] with Subject {
override def copyWithNewId(id : Long) : Entity[Long] = this.copy(id = id)
}
但是,如果我可以 - 使用一些 Scala 技巧 - 定义我的 <Model>Row 案例类子类,该子类是可复制的,并且除了传递的 idi.e 之外,它会复制自身。 IdCopyable 和copy(id = id) 那么我不需要为每个<Model>Row 生成的案例类一遍又一遍地实现这个copyWithNewId。
对于任何包含id 属性的案例类,有没有办法抽象或“拉起”重构copy(id = id)?还有其他推荐的解决方案吗?
更新 1 以下内容大致概括了我遇到的问题:
scala> abstract class BaseA[A <: BaseA[_]] { def copy(id : Int) : A }
defined class BaseA
scala> case class A(id: Int) extends BaseA[A]
<console>:12: error: class A needs to be abstract, since method copy in class BaseA of type (id: Int)A is not defined
case class A(id: Int) extends BaseA[A]
^
scala> case class A(id: Int); val a = A(5); a.copy(6)
defined class A
a: A = A(5)
res0: A = A(6)
更新 2 使用下面建议的解决方案,我得到以下编译错误:
[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDaoAutoIncImpl.scala:26: could not find implicit value for parameter gen: shapeless.Generic.Aux[E,Repr]
[error] val insertQuery = tableQuery returning tableQuery.map(_.id) into ((row, id) => row.copyWithNewId(id))
[error] ^
[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDaoAutoIncImpl.scala:27: value id is not a member of insertQuery.SingleInsertResult
[error] db.run((insertQuery += entity).flatMap(row => findById(row.id)))
[error] ^
[error] two errors found
更新 3 使用和调整以下建议的镜头解决方案我收到以下编译器错误:
import shapeless._, tag.@@
import shapeless._
import tag.$at$at
/**
* Identifyable base for all Strong Entity Model types
* @tparam PK Primary key type
* @tparam E Actual case class EntityRow type
*/
trait AutoIncEntity[PK, E <: AutoIncEntity[PK, E]] extends Entity[PK] { self: E =>
//------------------------------------------------------------------------
// public
//------------------------------------------------------------------------
/**
* Returns the entity with updated id as generated by the database
* @param id The entity id
* @return the entity with updated id as generated by the database
*/
def copyWithNewId(id : PK)(implicit mkLens: MkFieldLens.Aux[E, Symbol @@ Witness.`"id"`.T, PK]) : E = {
(lens[E] >> 'id).set(self)(id)
}
}
然后我得到以下编译器错误:
[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDaoAutoIncImpl.scala:26: could not find implicit value for parameter mkLens: shapeless.MkFieldLens.Aux[E,shapeless.tag.@@[Symbol,String("id")],PK]
[error] val insertQuery = tableQuery returning tableQuery.map(_.id) into ((row, id) => row.copyWithNewId(id))
[error] ^
[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDaoAutoIncImpl.scala:27: value id is not a member of insertQuery.SingleInsertResult
[error] db.run((insertQuery += entity).flatMap(row => findById(row.id)))
[error] ^
【问题讨论】: