2012-04-22 57 views
3

我使用的是極大的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 
} 

我無法實現使用的修真querys,因爲描述表中的元組是不是在性狀編譯時間(或抽象類)RecordRepository聞名。

在此先感謝!

+0

根據所提供的代碼片段,你還沒有映射的產品和訂單的模式類對應的數據庫表 – virtualeyes 2012-04-22 15:18:16

+0

是的,我想節省空間,在這裏一個簡單的模式: '對象的productTable延伸ExtendedTable [(長,字符串,整數)( 「產品」){ 高清ID =列[龍]( 「ID」,O.的PrimaryKey,O.AutoInc) DEF命名=柱[字符串]( 「姓名」,O. (Long,String,Int)](「訂單」))。請注意,如果訂單數量大於或等於零,則訂單數量將增加。 { DEF ID =柱[龍]( 「ID」,O.PrimaryKey,O.AutoInc) DEF命名=柱[字符串]( 「姓名」,O.NotNull) DEF量=柱[INT](「價格」 O.NotNull) 高清* = ID〜名〜量 } ' – singy 2012-04-22 18:12:37

回答

1

@singy OK,好,還以爲你離開了(映射)了(你應該修改你的答案,而把映射評論,順便說一句)。

嘗試以下操作:
1)改變class Productcase class Product
2)更換ExtendedTable[(Long, String, Int)]用,ExtendedTable[Product]

+0

非常感謝,這是正確的暗示! – singy 2012-04-23 17:38:25

+0

@singy,認爲元組是罪魁禍首;-) – virtualeyes 2012-04-23 17:55:56