tags:

views:

188

answers:

1

In my Grails domain I have something like the following

class A {
    String name
    static hasMany = [ b : B ]
    SortedSet b
    static fetchMode = [ b:"eager" ]
}

class B implements Comparable{
    A a
    ... compareTo method defined ....
}

What I'm trying to do is to retrieve an instance of class A, and at the same time have its collection (b) populated.

So I do A.get(1), expecting b to be populated,

but b.each(){ println it }

tells me that I'm calling each on a null object

I fear that I am fundamentally missing the point here, but I can't see what I'm doing wrong.

I'm running this as as integration test, against a MySql database. The database appears to be populated with data that would allow the association between A and B.

Any help greatly appreciated.

Dave

A: 

Maybe you need

static mapping = {
b lazy:false }
Derek
Sorry, to answer your original question. The fetch mode tells hibernate to do a join or another select when generating the query for the db. you might see something like "b fetch:'join'" which will tell hibernate to use a join when getting all of b instead of another select"
Derek
Thanks Derek - that's confirmed what I thought. I've also tried the solution you suggested, still with no joy. Are these two ways of specifying the fetchmode equivalent, or has one superseded the other?
Hi Dave, I think one has not superseded the other. there is a slight difference. if you use lazy:false it will eager fetch using another SELECT query. if you use fetch:join it will eager fetch (you dont need lazy:false) using a JOIN query. Not sure why you are getting no data back. Are you populating the DB with a bootstrap script or manually with sql before the test?
Derek
Bit more information - I was setting up the data in Bootstrap.groovy and then running the integration test. I've changed the datasource so that now it just uses data that is already stored in the database, and the tests are working. So it seems my understanding of FetchMode was OK - it was ( and remains ) my understanding of the interaction between bootstrap.groovy and integration tests that is at fault.
Hi Dave, Yes the Bootstrap.groovy is not ran for integration tests I think. This caused me much confusion once too. I had to manually setup test data in a beforeTest method. I heard something about grails bootstrapping integration tests in future. It would be handy if it did!
Derek
Well, from my observation it appears that it _does_ run the bootstrap, and the data is saved successfully to the database. It just can't be seen by the subsequent retrievals. I don;t know much about Hibernate, but I do seem to remember there being something about a Hibernate session - does this need to be flushed or something?
Hi Derek - to round this up, bootstrap.groovy is run for integration tests, but you need to flush the session to make the changes visible to the subsequent tests. This link pointed me in the right direction :http://old.nabble.com/Advanced-integration-test-question-td23116535.html