scala - 使用ScalaQuery的通用存储库

标签 scala generics repository scalaquery

我正在使用出色的ScalaQuery,并试图为常规操作创建通用存储库,但我对此并不满意。希望有人可以提供帮助。

我有例如以下结构

abstract class Record(id : Int) 
class Product(id: Int,name:String,price : Int) extends Record(id)
class Order(id: Int,name:String,amount: Int)  extends Record(id)

以及用于产品和订单的拖车表。现在我想要通用存储库:
trait RecordRepository[T <: ExtendedTable[O]]{
     findById(id:Int) : Option[T]
     findAll() : List[T]
     save(entity:T)
     ...
}      

class ProductRepository extends RecordRepository[Product]
class ProductRepository extends RecordRepository[Order]

object ProductTable extends ExtendedTable[(Long, String, Int)]("product") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc) 
  def name = column[String]("name", O.NotNull) 
  def price = column[Int]("price", O.NotNull) 
  def * = id ~ name ~ price 
} 
object OrderTable extends ExtendedTable[(Long, String, Int)]("order") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc) 
  def name = column[String]("name", O.NotNull)
  def amount = column[Int]("price", O.NotNull) 
  def * = id ~ name ~ amount
}

我无法使用for理解来实现查询,因为描述该表的元组在特征(或抽象类)RecordRepository中的编译时是未知的。

提前致谢!

最佳答案

@singy好的,很好,以为您忽略了(映射)(您应该编辑答案,而不是在注释中添加映射,顺便说一句)。

请尝试以下操作:
1)将class Product更改为case class Product2)用ExtendedTable[(Long, String, Int)]替换ExtendedTable[Product]

关于scala - 使用ScalaQuery的通用存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268428/

相关文章:

rest - 获取重骨料列表

scala - Akka Streams 按类型拆分流

scala - Spark 中有哪些不同的联接类型?

swift - 使用通用约束

repository - Artifactory Rest API 完全限定类搜索

git 推送的文件不会显示在 cPanel 文件管理器中

function - 这是成语 Scala.使用 def 来缩短语句?

azure - Databricks parquet 读取时间太长

generics - Kotlin:覆盖子类型中的通用属性

c# - 为什么基类规范的含义不能递归地依赖于 C# 中的自身?