One-to-one relationship with lift Mapper
Question
How do I implement a One-to-One relationship with Mapper? From Lift wiki:
If you’re looking to model a one-to-one relationship, just use a one-to-many relationship. The only potential hassle is that you’ll have a List[B] instead of a Box[B].
Isn't there a more idiomatic way?
Solution
I might approach it by reducing the visibility of the one-to-many relationship and create a getter/setter as such:
protected object comments extends
MappedOneToMany(Comment, Comment.post, OrderBy(Comment.id, Ascending))
def comment : Option[Comment] = {
comments match {
case Nil => None
case head :: _ => Some(head)
}
}
def comment_=(comment: Comment) = {
comments.clear
comments += comment
}
Ideal? No. But more idiomatic than having client code deal with the List[Comment] instead of Option[Comment]? I think so.
Along this same line, you could also create your own class which extends MappedOneToMany. This code is untested, but I believe in the spirit of it:
class MappedOneToOne[K,T<:KeyedMapper[K, T],O <: Mapper[O]]
(meta: MetaMapper[O], foreign: MappedForeignKey[K,O,T], qp: QueryParam[O]*) extends MappedOneToMany(meta, foreign, qp) {
def get : Option[O] = {
all match {
case Nil => None
case head :: _ => Some(head)
}
}
def set(o: O) : O = {
clear
this += o
o
}
}
class Foo extends KeyedMapper[Int,Foo] {
object bar extends MappedOneToOne[Int,Foo,Bar]
}
f.bar.get match {
case Some(bar) => println("Got bar")
case _ =>
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow