What is the best way to deal with compsite keys when using Salat with MongoDB?

一笑奈何 提交于 2019-12-05 07:23:50

main Salat developer here.

Like Milan suggested, create a case class for your composite key:

case class FooKey(someRelatedId: String, email: String)

case class Foo(@Key("_id") naturalKey: FooKey) {

  // use @Persist if you want these fields serialized verbatim to Mongo - see https://github.com/novus/salat/wiki/Annotations for details
  @Persist val email =  naturalKey.email
  @Persist val someRelatedId = naturalKey.someRelatedId

}

object FooDAO extends SalatDAO[Foo, FooKey](collection = /*  some Mongo coll */ )

If you object to "_id" as a field name, you can use a global override in the context to remap "_id" to "naturalKey", or supply ad hoc @Key overrides on each object.

I don't personally like giving the _id a different name in your models as then your Mongo queries must use the serialized key "_id" while all your business logic must use the case class field name ("naturalKey" or whatever), but YMMV.

P.S. Our mailing list is at http://groups.google.com/group/scala-salat - I'll see your question quicker there than Stack Overflow.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!