tags:

views:

113

answers:

3

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.

+1  A: 

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
}
Rich Kroll
+1 for your suggestion. failOnError shows validation error.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.bsr.test.A] for property 'A': no matching editors or conversion strategy found]I don't understand why treat the object as String, as I explained, in my IDE the object is filled correctly after the assignment. I'm looking at BindData or some other method to add this complex field.
bsreekanth
Without seeing more code from your application, I cannot help diagnose this additional problem. It appears that at the time of validation that the variable is a String, when the object is expected.
Rich Kroll
A: 

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

bsreekanth
If that did indeed solve your issue, could you close this post?
Rich Kroll
A: 

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.

Edvinas Bartkus
thanks.. you are right, the interface was wrongly used in my example. the reap problem was, in my xml (see I create the object parsing the xml, so i don't have the id) and I use something like ..<?xml version="1.0"?><B something="1" A="name of A"\>Here the problem is when I do def b = new B(xml.attributes()) , the constructor try to map A (which should have looked up seperately, as I still don't know the id)..
bsreekanth