2012-02-18 63 views
0

UPDATE:改變了樣本,以反映我目前的狀況電梯映射 - 特質與一對多映射

相當新的提升,我想創建我的應用程序的模型。由於我想保持DRY精神,所以我想使用trait mixins來指定模型中的一些字段。舉例來說,我有一個Person特質,我MIXIN我Employee類:

trait Person[T <: LongKeyedMapper[T]] extends LongKeyedMapper[T]{ 
    self: T => 
    object firstName extends MappedString[T](this, 50) 
    object lastName extends MappedString[T](this, 50) 
    object civicRegNumber extends MappedString[T](this, 12) 
} 

class Employee extends IdPK with OneToMany[Long, Employee] with Person[Employee] { 
    def getSingleton = Employee 

    object contactInfos extends MappedOneToMany(EmployeeContactInfo, EmployeeContactInfo.person) 
} 

object Employee extends Employee with LongKeyedMetaMapper[Employee] 

可以看出我有很多在員工一個映射contactInfos。它看起來像:

trait PersonContactInfo[T <: LongKeyedMapper[T],P <: Person[P]] extends LongKeyedMapper[T] { 
    self: T => 
    object email extends MappedEmail[T](this, 80) 
    def personMeta:P with LongKeyedMetaMapper[P] 
    object person extends LongMappedMapper[T,P](this, personMeta) 
} 

class EmployeeContactInfo extends IdPK with PersonContactInfo[EmployeeContactInfo, Employee] { 
    def getSingleton = EmployeeContactInfo 
    val personMeta = Employee 

} 
object EmployeeContactInfo extends EmployeeContactInfo with LongKeyedMetaMapper[EmployeeContactInfo] 

這似乎是工作,但我想移動的對象contactInfos到我Person特質。然而,我無法弄清楚如何實現這個...是否可以繼承OneToMany映射?任何幫助歡迎!

回答

1

經過幾次嘗試之後,我通過將Person的OneToMany映射到PersonContactInfo的特徵分開來得到了這個結果。這是現在的樣子

trait Person[T <: Person[T]] extends LongKeyedMapper[T]{ 
    self: T => 
    object firstName extends MappedString[T](this, 50) 
    object lastName extends MappedString[T](this, 50) 
    object civicRegNumber extends MappedString[T](this, 12) 
} 

trait PersonToPersonContacts[P <: Person[P], PCI <: PersonContactInfo[PCI, P]] extends OneToMany[Long,P] { 
    self: P => 
    def contactInfoMeta:LongKeyedMetaMapper[PCI] with PCI 
    object contactInfos extends MappedOneToMany(contactInfoMeta, contactInfoMeta.person) 
} 

class Employee extends IdPK with Person[Employee] with PersonToPersonContacts[Employee, EmployeeContactInfo] { 
    def getSingleton = Employee 
    override def contactInfoMeta = EmployeeContactInfo 
} 
object Employee extends Employee with LongKeyedMetaMapper[Employee] 

和我PersonContactInfo現在看起來像:

trait PersonContactInfo[T <: LongKeyedMapper[T],P <: Person[P]] extends LongKeyedMapper[T] { 
    self: T => 
    object email extends MappedEmail[T](this, 80) 
    def personMeta:P with LongKeyedMetaMapper[P] 
    object person extends LongMappedMapper[T,P](this, personMeta) 
} 

class EmployeeContactInfo extends IdPK with PersonContactInfo[EmployeeContactInfo, Employee] { 
    def getSingleton = EmployeeContactInfo 

    val personMeta = Employee 

} 
object EmployeeContactInfo extends EmployeeContactInfo with LongKeyedMetaMapper[EmployeeContactInfo] 

仍然不知道這是解決這個問題的方法,但是,兩者均沒有工作:)