Niels Bech Nielsen
Niels Bech Nielsen

Reputation: 4859

Easy save or update of hierarchical object gorm

How can I easily deal with nested properties in GORM?

If I have a map of properties including nested properties:

def mymap = [
   id : '1',
   name : 'first name',
   subs : [[
      subid : 1,
      name : 'first sub name'
   ],[
      subid : 2,
      name : 'second sub name'
   ]]
]

And I have the following domain objects:

class Node {
  int id
  String name
  static hasMany = [subs:Sub]
}
class Sub {
  int id
  String name
}

If I create a new node with the map

new Node(myMap).save()

It complains with something like >> Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.Set' for property 'subs'

Same goes for updates when I try with

node.properties = myMap
node.save()

Is there no way I can do this automatically but have to traverse it manually?

Upvotes: 0

Views: 419

Answers (2)

rimero
rimero

Reputation: 2383

Your map contents look more or less like some JSON.

My first try (if it were failing out of the box) would be to try to use JSON converters and if that still fails, I would probably hook my JSON marshaller for the Node type.

Upvotes: 1

Mr. Cat
Mr. Cat

Reputation: 3552

def mymap = [
   id : '1',
   name : 'first name',
   subs : [[
      subid : 1,
      name : 'first sub name'
   ],[
      subid : 2,
      name : 'second sub name'
   ]] as Set
]

Upvotes: 0

Related Questions