You are currently browsing legacy 4.1 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Loading entities

The load method allow user to download a documents from database and convert them to entities.

Syntax

def load(self, key_or_keys, object_type=None, includes=None, nested_object_types=None):
Parameters
key_or_keys string or list Identifier of a document\s that will be loaded.
object_type classObj The class we want to get
includes list or str The path to a reference inside the loaded documents can be list (property name)
nested_object_types dict {str : classObj} A dict of classes for nested object the key will be the name of the class and the value will be the object we want to get for that attribute.
Return Value
object_type or None instance of object_type or None if document with given Id does not exist. (If the object_type equal to None return DynamicStructure entity)

Example I

with store.open_session() as session:
    # dynamic_employee will be a DynamicStructure
    employee = session.load("employees/1")

In this example the load will return DynamicStructure entity because we don't specify any classObj in object_type

Example II

A load with object_type:

with store.open_session() as session:
    # employee will be Employee object
    employee = session.load("employees/1", object_type=Employee )

Important

class Student(object):
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

class Teacher(object):
    def __init__(self, first, last):
        self.first_name = first
        self.last_name = last

with store.open_session() as session:
    # student will be Student object
    student = session.load("employees/1", object_type=Student)

    teacher = session.load("employees/1", object_type=Teacher)
    # Will raise TypeError because the way we convert the document to entity

If the class variables names are different from the method signature name. TypeError exception will be raise

load - multiple entities

To load multiple entities at once use the load just pass to key_or_keys a list of keys.

with store.open_session() as session:
    keys_to_load = ["employees/1", "employees/2", "employees/3"]
    employee = session.load(keys_to_load, object_type=Employee )

In this example the load method will return a list with the specific demanded documents

Note

If some document couldn't be loaded we will put None in the list

load with includes

When there is a 'relationship' between documents, then those documents can be loaded in a single request call using load.

class Order(object):
    def __init__(self, company):
        self.company = company


class Company(object):
    def __init__(self, name, address):
        self.name = name
        self.address = address

with store.open_session() as session:
    session.store(Company("Hibernating Rhinos", "Israel" ))
    session.store(Order(company="companies/1"), key="orders/1")
    session.save_changes()

with store.open_session() as session:
    order = session.load("orders/1", object_type=Order, includes="company")

The return value will be the same but next time we will try to load an include document we will get the document without doing any requests to the server

load with nested object types

Sometimes we will use some objects that have nested object with load you can specify your nested object type and try to convert those to the right type.

class Dog(object):
    def __init__(self, brand, color, age):
        self.brand = brand
        self.color = color
        self.age = age


class Car(object):
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color


class Person(object):
    def __init__(self, name, dog, car):
        self.name = name
        self.dog = dog  # instance of Dog
        self.car = car  # instance of Car

with store.open_session() as session:
    person = session.load("people/1", object_type=Person,
                          nested_object_types={"dog": Dog, "car": Car})