3

I have a tree structure similar to this in Grails:

class TreeNode {
  String name
  // more properties
  List children = []
  static hasMany = [children: TreeNode]
  static belongsTo = [parent: TreeNode, root: TreeNode]
  static mappedBy = [children:'parent']

  static constraints = {
    name(blank: false,maxsize: 100,)
    parent(nullable:true)
    root(nullable:false)
  }
}

For SQL performance reasons, I need each record to have a direct reference to its root node. Thus, the 'root' property.

Before I added 'root', the nodes saved correctly. That is, I could call save() on the root node, and the parent_id field was properly assigned in all nodes (except the root itself, whose parent ID remains null).

I have attempted to assign 'root' in beforeInsert() by walking the tree upward until I find the parent.

def beforeInsert = {
  def node = this
  while (node.parent) {
    node = node.parent
  }
  root = node 
}

I'm routinely getting stack overflow exceptions.

at org.codehaus.groovy.grails.orm.hibernate.validation.HibernateDomainClassValidator.cascadeValidationToOne(HibernateDomainClassValidator.java:116)
    at org.codehaus.groovy.grails.validation.GrailsDomainClassValidator.cascadeToAssociativeProperty(GrailsDomainClassValidator.java:142)

When the stack overflow exception does not occur, each 'root' references itself instead of the ultimate root. The beforeInsert() is not climbing the tree.

Can you help me break this problem down? Does GORM attempt to save my tree structure depth-first? If so, how does it save the parent ID if the parent hasn't yet been persisted? Most importantly, how can I save the tree by calling save() on the root node, such 'root' is set properly without having to update all the nodes (immediately) after the save()?

1
  • BTW, I'm using Grails 1.3.7 at present.
    – Jim Norman
    Jul 31, 2012 at 22:21

2 Answers 2

1

Well, by always keeping a root node updated you'll find yourself constantly iterating over the tree, which is the opposite you want by keeping a reference to the root node. But if you really want to keep a root node, you could try this structure:

Class TreeNode {
    String name
    TreeNode parent
    TreeNode root

    static hasMany = [children: TreeNode]

    TreeNode getRoot(){
       //of course, if parent is null, it means, you're already in the root node.
       if(parent){
          return parent.getRoot()
       }else{
          return this
       }
    }
}

So you don't have to worry about explicitly updating your root node every single time. Just set the children nodes and you're done. Root can be found dinamically (but again, you will be iterating in the tree one way or another). Plus it's a cleaner mapping, I guess ;)

EDIT: I put another reference (to root). So you can choose if you use the getRoot method to set it dinamically or not. In any case, every node will have a reference to the root.

1
  • As noted in the question, I need root_id to appear in the table. I rely on it for SQL performance considerations.
    – Jim Norman
    Aug 1, 2012 at 14:31
0

SO must be happening due to root referencing this, and validation going into an infinite loop. Try to change to:

if (!parent) {
  root = this
  return
}
def node = parent
while (node.parent) {
  node = node.parent
}
root = node

If a node is referencing itself and not THE root - it means you have parent (wrongly) not assigned.

GORM starts with the instance that you called save() on, and saves all its children (that belongsTo the instance) first. So, the tree should save on ultimateRoot.save().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.