如何更新/删除嵌入对象
给定以下域模型:
case class Benefits(id: Int, benefitPlan: String, comment : String)
case class Employee(empNum : Int, benefits : List[Benefit])
我一直在使用 Salat 来帮助序列化/反序列化这些对象。但是,鉴于我知道要删除/更新的对象的 Benefit.id,我对如何从员工对象的福利列表中删除/更新特定对象感到有点困惑?
我不想迭代完整的好处列表以便能够更新单个对象,因为该列表在运行时可能包含大量对象。有没有比获取 emp 对象、迭代列表直到找到所需对象、更新它然后将 emp 对象保存回来更好的方法?
Given following domain model:
case class Benefits(id: Int, benefitPlan: String, comment : String)
case class Employee(empNum : Int, benefits : List[Benefit])
I've been using Salat to help derialize/deserialize these objects. However, I'm a little confused as to how to delete/update a specific object from the benefits List in employee object given that I know the benefit.id of the object that is to be deleted/updated?
I do not want to iterate the full benefits list to be able to update a single object since this list may hold large number of objects at runtime. Is there a better way than getting the emp object, iterating the list until desired object is found, update it and then save the emp object back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,我想说您的好处应该是Map[Int, Benefits]。
如果您使用
Map
,您的更新/删除将是 O(1) 而不是线性时间。如果您需要随机访问和更新其中的元素,
List
不是一个好的选择。In this case, I would say your
benefits
should be anMap[Int, Benefits]
.If you use
Map
, your update/delete will be O(1) instead of linear time.List
is not a good choice if you need randomly access and update an element in it.我建议查看这篇文章 关于 Casbah 和 Salat(假设您在 MongoDB 中使用 salat)
I'd suggest looking into this article about Casbah and Salat (assuming you're using salat for MongoDB)