我正在使用出色的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 Product
2)用ExtendedTable[(Long, String, Int)]
替换ExtendedTable[Product]
关于scala - 使用ScalaQuery的通用存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268428/