开发者

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
  3. 开发者_如何转开发

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.


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
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜