2012-02-18 49 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] 

仍然不知道这是解决这个问题的方法,但是,两者均没有工作:)