斯卡拉 – OneToMany与Lift的关系
发布时间:2020-12-16 09:05:08 所属栏目:安全 来源:网络整理
导读:我再次提出一个问题,我觉得解决方案很简单.无论如何应该简单.我一直在尝试为我的数据库建模我的映射,具有漂亮的OneToMany特性,就像这样 class User extends MegaProtoUser[User] with OneToMany[Long,User] { def getSingleton = User object posts extends
我再次提出一个问题,我觉得解决方案很简单.无论如何应该简单.我一直在尝试为我的数据库建模我的映射,具有漂亮的OneToMany特性,就像这样
class User extends MegaProtoUser[User] with OneToMany[Long,User] { def getSingleton = User object posts extends MappedOneToMany(Post,Post.author,OrderBy(Post.edited,Descending)) } class Post extends LongKeyedMapper[Post] with IdPK with OneToMany[Long,Post] { def getSingleton = Post object author extends LongMappedMapper(this,User) object title extends MappedString(this,100) { override def dbIndexed_? = true override def defaultValue = "New Post" } object contents extends MappedText(this) object edited extends MappedDate(this) } 一切都很好,我有一对多的关系.但是,真正困扰我的是什么,我如何实际获得该关系中的数据?假设我有一个Post对象,想要得到作者的名字? bind("post",in,"title" -> post.title,"author" -> /* insert overly complicated method here */) 我已经知道我可以通过post.author.obj或其他方式获得它.它没有完全解决,我必须做一个匹配它等等? 我觉得答案很简单,但我却没有看到它. 解决方法
Mapper并不保证post.author总能成功,所以它必须返回一个Box.您可以使用post.author.obj.map(_.name.is)openOr“unknown name”或类似名称来避免使用匹配语句.
或者,您可以在Post上创建一个辅助方法,它执行默认处理并使用它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |