Salat author here.
Salat doesn't have anything like ORM lazy loading. The @Persist
annotation is meant to persist fields outside of the constructor, but suppresses deserialization because only fields in the constructor will be deserialized.
But you can easily decide when making the query whether you want the list of users or not.
case class Test(@Key("_id") id = new ObjectId, name: String, users: List[User] = Nil)
You can persist the users as embedded documents inside the test document, and then use the second argument of the query, the ref, to exclude (0) or include (1) fields in the object.
TestDAO.find(/* query */, MongoDBObject("users" -> 0))
The other strategy is to break out user documents into a child collection - see https://github.com/novus/salat/wiki/ChildCollection for more information. In this example, Test
is the "parent" and User
is the "child".
The strategy there is that in the parent DAO, when saving, you override the save methods to save users using the child DAO, and then save the parent object with users
set to Nil
.
Then, by default, a Test
instance is retrieved with users
set to Nil.
If you want to retrieve Test
with users
, you will need to add a find method to your DAO that manually:
- find the test document
- use the
_id
field of the test document to query for user documents by parent id - this will yieldList[User]
- deserialize the test document to an instance of
Test
usinggrater[Test]
and copy it with the list of users