Session: How to Get and Modify Entity Metadata

When a document is downloaded from the server, it contains various metadata information like ID or current change-vector. This information is stored in a session and is available for each entity using the getMetadataFor method from the advanced session operations.

Get the Metadata

<T> IMetadataDictionary getMetadataFor(T instance);
Parameters
instance T Instance of an entity for which metadata will be returned.
Return Value
IMetadataDictionary Returns the metadata for the specified entity. Throws an exception if the instance is not tracked by the session.

Example

Employee employee = session.load(Employee.class, "employees/1-A");
IMetadataDictionary metadata = session.advanced().getMetadataFor(employee);

Modify the Metadata

After getting the metadata from session.advanced().getMetadataFor you can modify it just like any other map.

Note

Keys in the metadata that starting with @ are reserved for RavenDB use

Example I

User user = new User();
user.setName("Idan");

session.store(user);

IMetadataDictionary metadata = session.advanced().getMetadataFor(user);
metadata.put("Permissions", "READ_ONLY");
session.saveChanges();

Example II

User user = session.load(User.class, "users/1-A");
IMetadataDictionary metadata = session.advanced().getMetadataFor(user);
metadata.put("Permissions", "READ_AND_WRITE");
session.saveChanges();