Lift Mapper - 具有 OneToMany 映射的特征
更新:更改示例以反映我当前的情况
相当新,我正在尝试为我的应用程序创建一个模型。因为我想保持 DRY 的精神,所以我想使用特征混合来指定模型中的一些字段。例如,我有一个 Person
特征,我将其混合到我的 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]
可以看出,我在 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 映射?欢迎任何帮助!
UPDATE: Changed the samples to reflect my current situation
Fairly new to lift and I am trying to create a model for my application. Since I want to keep things in the spirit of DRY I want to use trait mixins to specify some of the fields in my model. For instance, I have a Person
trait which I mixin to my Employee
class:
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]
As can be seen I have a contactInfos many to one mapping in the Employee. It looks like:
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]
This seems to work, but I would like to move the contactInfos object into my Person
trait. However, I can't figure out how to achieve this... Is it at all possible to inherit OneToMany mappings from traits? Any help welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几次尝试后,我通过分离执行 Person 到 PersonContactInfo 的 OneToMany 映射的特征来实现此目的。这就是它现在的样子
,我的 PersonContactInfo 现在看起来像:
仍然不确定这是否是解决这个问题的方法,但 ity 可以完成这项工作:)
After a few tries I got this to work by separating the trait which does the OneToMany mapping of Person to PersonContactInfo. This is how it looks now
and my PersonContactInfo now looks like:
Still not sure if this is the way to solve this, but ity does the job :)