自定义Slick Code Generator 3.0.0

时间:2015-06-25 11:14:22

标签: scala slick scala-2.11 slick-3.0 slick-codegen

可以使用光滑的codegen生成${container} trait之外的所有映射的case类,以便它们不会继承它的类型吗?也许在另一个文件中,即Models.scala

// SuppliersRowsDAA.scala
import persistence.Tables

object SuppliersRowsDAA {
  case class Save(sup: Tables.SuppliersRow)
}   

我收到此编译错误:

[error] /app/src/main/scala/persistence/dal/SuppliersDAA.scala:5: type mismatch;
[error]  found   : persistence.Tables.SuppliersRow
[error]  required: SuppliersDAA.this.SuppliersRow
[error]     case Save(sup) ⇒ sender ! db.run(Suppliers += sup)

使用Tables#SuppliersRow导入会产生同样的错误。

如果我手动切割&将SuppliersRow案例类粘贴到自动生成的trait Tables之外!

....
trait Tables {
....
}
case class SuppliersRow(id: Int, userId: Int, name: String)
//EOF

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

docWithCode的原始EntityType追加到super.packageCode()

import slick.codegen.SourceCodeGenerator
import slick.model.Model
import scala.collection.mutable

class CustomizedCodeGenerator(model: Model) extends SourceCodeGenerator(model) {
  val models = new mutable.MutableList[String]

  override def packageCode(profile: String, pkg: String, container: String, parentType: Option[String]): String = {
    super.packageCode(profile, pkg, container, parentType) + "\n" + outsideCode
  }

  def outsideCode = s"${indent(models.mkString("\n"))}"

  override def Table = new Table(_) {
    override def EntityType = new EntityTypeDef {
      override def docWithCode: String = {
        models += super.docWithCode.toString + "\n"
        ""
      }
    }
  }

}