Lift Mapper - 具有 OneToMany 映射的特征

发布于 2025-01-07 00:37:29 字数 1474 浏览 1 评论 0原文

更新:更改示例以反映我当前的情况

相当新,我正在尝试为我的应用程序创建一个模型。因为我想保持 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2025-01-14 00:37:29

经过几次尝试后,我通过分离执行 Person 到 PersonContactInfo 的 OneToMany 映射的特征来实现此目的。这就是它现在的样子

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]

仍然不确定这是否是解决这个问题的方法,但 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

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] 

and my PersonContactInfo now 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]

Still not sure if this is the way to solve this, but ity does the job :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文