google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Add loadRootRef param to @Reference annotation #279

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
My enhancement proposition is to add another boolean param (loadRootRef) to 
@Reference annotation. It would be usable only for recursive references (like 
trees).

Let's take simple class:

@Entity
class Node {
    @Reference
    Node root;

    @Reference
    Set<Node> children;
}

Imagine we have million-node tree in database and we want to fetch one Node 
only. We can use lazy = true to achieve that - but in use case where we iterate 
on all nodes and do something simple on referenced nodes (for example count 
children) this would produce excesive amount of queries because of lazy loading.

With loadRootRef = true param:
@Entity
class Node {
    @Reference(lazy = true, loadRootRef = true)
    Node root;

    @Reference(lazy = true, loadRootRef = true)
    Set<Node> children;
}

Morphia would load desired Node, it's root and children but would NOT load 
children of children, root of root, children of root, root of children etc.

Original issue reported on code.google.com by MomotDam...@gmail.com on 23 May 2011 at 3:33