Skip to content

Coping with Lazy Initialization

lukdumi edited this page Aug 18, 2014 · 10 revisions

General idea

By default all referenced objects in the saos model are marked as lazy. Therefore, repository standard find|get methods return only object 'stubs', i.e. all objects referenced from the fetched object are not initialized.

If you need to get fully initialized object then use special repository methods designed for it. Their names end with 'AndInitialize'. Use them carefully because there are often dozens of databases query executions needed to initialize the whole object tree.

Example:

judgmentRepository.findOne(String id) - gets uninitialized Judgment object judgmentRepository.findOneAndInitialize(String id) - gets Judgment object with initialized sub-objects, e.g. judges and court.

In some cases you will want only some specific attributes initialized - if it is justifiable - write your own custom query.

Full initialization implementation

The 'AndInitialize' methods use the InitializingVisitor internally. The visior is passed to the accept method of the given DataObject. The accept method does two things:

  • executes a proper (depending on passed argument) 'visit' method of the passed visitor
  • invokes 'passVisitorDown' method which passes the visior to referenced objects.

[You will find more information on these two methods in their comments]

If you want to initialize a new sub-object of the given object-tree you should do one of 2 steps:

  • if the object is an exclusive part of the tree - pass the visitor to this object (use the passVisitorDown of the referencing object)
  • if the object is 'outside' the tree or you cannot pass the visitor to it (because it is not a DataObject) - write specialized visit method in the InitializingVisitor.