bsr
bsr

Reputation: 58742

Grails : data binding

i'm trying to create some domain objects from xml.

class A {
  String name
}
class B {
  A a
  int something
}

i first created an instance of A,and flushed. when creating B, first map the available attributes.

def b = new B(xml.attributes())

this would map 'something' correctly, but not the object type A. So, I retrieve the instance of A and add like

 b.a = A.findByA("id of a")

I could see the object b is constructed (both fields filled in) in the debugger, but it doesn't persist on save(flush:true).

  1. What is wrong in the above assignemt, or should use the id instead (b.a.id = ..)
  2. How can I see what is going wrong in the log file? which trace needs to be enabled. I enabled there in config file

trace 'org.hibernate.SQL', 'org.hibernate.type' (which gives the sql trace for insert, select etc. But not for the above scenario, may be because it doesn't reach to hibernate).

Any pointer, highly appreciated.. thanks.

Upvotes: 0

Views: 859

Answers (3)

Edvinas Bartkus
Edvinas Bartkus

Reputation: 467

To create association you must pass an object of A

new B(a:A.get(id))

or

B b = new B()
b.a = A.get(id)

Where id must be Integer or Long

Either I miss some context but class A doesn't have method findByA. There is no such A attribute for class A. Suggest you to use method get for strict findings.

Upvotes: 0

bsr
bsr

Reputation: 58742

With the debugging tip from Rich, I could narrow down the problem... had to rename the attribute to prevent auto mapping. See a similar issue, and response at http://grails.1312388.n4.nabble.com/domain-controller-and-Failed-to-convert-property-value-of-type-problem-td1357947.html

Upvotes: 0

Rich Kroll
Rich Kroll

Reputation: 4005

I would wager to guess that your save() is failing validation. You can add save(failOnError:true) to throw an exception when the validation fails, or add the following code to print each of the errors:

b.errors.allErrors.each {
    println it
}

Upvotes: 1

Related Questions