Quantcast

GORM Gotchas - Being eager

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

GORM Gotchas - Being eager

jl_monteagudo
Hello all,

I'm writing this post because I have followed Peter Ledbrook's post about GORM Gotchas Part 3, and I'm getting an unexpected result when I test the fetch:join mapping. The problem is that Hibernate is accessing more than one time to the database, albeit I have mapped a relation with the fecth:join option.

I have the following two classes:


class Location {
        String city
}


class Author {
    String name
    Location location
 
    static hasMany = [ books: Book ]
       
        static mapping = {
                location fetch: 'join'
        }
}


Then I run the following script in the console:

if (Author.count() == 0) {

    def location1 = new Location(city:"location1").save(flush:true)
    def location2 = new Location(city:"location2").save(flush:true)
    def location3 = new Location(city:"location3").save(flush:true)
   
    new Author(name:"name1", location:location1).save(flush:true)
    new Author(name:"name2", location:location1).save(flush:true)
    new Author(name:"name3", location:location1).save(flush:true)
    new Author(name:"name4", location:location2).save(flush:true)
    new Author(name:"name5", location:location3).save(flush:true)

}

Author.list().each { author ->
    println author.location.city
}


The first time that I run the script I get the following SQL log:

Hibernate: select count(*) as y0_ from author this_
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null, ?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null, ?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null, ?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null, ?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null, ?, ?, ?)

Hibernate: select this_.id as id1_1_, this_.version as version1_1_, this_.location_id as location3_1_1_, this_.name as name1_1_, location2_.id as id0_0_, location2_.version as version0_0_, location2_.city as city0_0_ from author this_ inner join location location2_ on this_.location_id=location2_.id

location1
location1
location1
location2
location3


The second time that I run the script I get:

Hibernate: select count(*) as y0_ from author this_

Hibernate: select this_.id as id1_1_, this_.version as version1_1_, this_.location_id as location3_1_1_, this_.name as name1_1_, location2_.id as id0_0_, location2_.version as version0_0_, location2_.city as city0_0_ from author this_ inner join location location2_ on this_.location_id=location2_.id

location1
location1
location1
location2
location3

And the following times I always get:

Hibernate: select count(*) as y0_ from author this_

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_, author0_.location_id as location3_1_1_, author0_.name as name1_1_, location1_.id as id0_0_, location1_.version as version0_0_, location1_.city as city0_0_ from author author0_ inner join location location1_ on author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_, author0_.location_id as location3_1_1_, author0_.name as name1_1_, location1_.id as id0_0_, location1_.version as version0_0_, location1_.city as city0_0_ from author author0_ inner join location location1_ on author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_, author0_.location_id as location3_1_1_, author0_.name as name1_1_, location1_.id as id0_0_, location1_.version as version0_0_, location1_.city as city0_0_ from author author0_ inner join location location1_ on author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_, author0_.location_id as location3_1_1_, author0_.name as name1_1_, location1_.id as id0_0_, location1_.version as version0_0_, location1_.city as city0_0_ from author author0_ inner join location location1_ on author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_, author0_.location_id as location3_1_1_, author0_.name as name1_1_, location1_.id as id0_0_, location1_.version as version0_0_, location1_.city as city0_0_ from author author0_ inner join location location1_ on author0_.location_id=location1_.id where author0_.id=?

location1
location1
location1
location2
location3


I don't understand why the second time that I run the script Hibernate accesses only one time to the database, and why the third and next times that I run the script Hiberante accesses 5 times to the database.

Can somebody explain me this behavior?

Thank you very much!!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM Gotchas - Being eager

rosenfeld
I guess this is the same bug I've reported a few days ago:

http://jira.grails.org/browse/GRAILS-8888

Please, confirm if that is what's happening to you.

Cheers,
Rodrigo.

Em 07-03-2012 18:56, jl_monteagudo escreveu:
Hello all,

I'm writing this post because I have followed Peter Ledbrook's post about
GORM Gotchas Part 3, and I'm getting an unexpected result when I test the
*fetch:join* mapping. The problem is that Hibernate is accessing more than
one time to the database, albeit I have mapped a relation with the
fecth:join option.

I have the following two classes:


class Location {
	String city
}


class Author {
    String name
    Location location
 
    static hasMany = [ books: Book ]
	
	static mapping = {
		location fetch: 'join'
	}
}


Then I run the following script in the console:

if (Author.count() == 0) {

    def location1 = new Location(city:"location1").save(flush:true)
    def location2 = new Location(city:"location2").save(flush:true)
    def location3 = new Location(city:"location3").save(flush:true)
    
    new Author(name:"name1", location:location1).save(flush:true)
    new Author(name:"name2", location:location1).save(flush:true)
    new Author(name:"name3", location:location1).save(flush:true)
    new Author(name:"name4", location:location2).save(flush:true)
    new Author(name:"name5", location:location3).save(flush:true)

}

Author.list().each { author ->
    println author.location.city
}


The first time that I run the script I get the following SQL log:

Hibernate: select count(*) as y0_ from author this_
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into location (id, version, city) values (null, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null,
?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null,
?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null,
?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null,
?, ?, ?)
Hibernate: insert into author (id, version, location_id, name) values (null,
?, ?, ?)

Hibernate: select this_.id as id1_1_, this_.version as version1_1_,
this_.location_id as location3_1_1_, this_.name as name1_1_, location2_.id
as id0_0_, location2_.version as version0_0_, location2_.city as city0_0_
from author this_ inner join location location2_ on
this_.location_id=location2_.id

location1
location1
location1
location2
location3


The second time that I run the script I get:

Hibernate: select count(*) as y0_ from author this_

Hibernate: select this_.id as id1_1_, this_.version as version1_1_,
this_.location_id as location3_1_1_, this_.name as name1_1_, location2_.id
as id0_0_, location2_.version as version0_0_, location2_.city as city0_0_
from author this_ inner join location location2_ on
this_.location_id=location2_.id

location1
location1
location1
location2
location3

And the following times I always get:

Hibernate: select count(*) as y0_ from author this_

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_,
author0_.location_id as location3_1_1_, author0_.name as name1_1_,
location1_.id as id0_0_, location1_.version as version0_0_, location1_.city
as city0_0_ from author author0_ inner join location location1_ on
author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_,
author0_.location_id as location3_1_1_, author0_.name as name1_1_,
location1_.id as id0_0_, location1_.version as version0_0_, location1_.city
as city0_0_ from author author0_ inner join location location1_ on
author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_,
author0_.location_id as location3_1_1_, author0_.name as name1_1_,
location1_.id as id0_0_, location1_.version as version0_0_, location1_.city
as city0_0_ from author author0_ inner join location location1_ on
author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_,
author0_.location_id as location3_1_1_, author0_.name as name1_1_,
location1_.id as id0_0_, location1_.version as version0_0_, location1_.city
as city0_0_ from author author0_ inner join location location1_ on
author0_.location_id=location1_.id where author0_.id=?

Hibernate: select author0_.id as id1_1_, author0_.version as version1_1_,
author0_.location_id as location3_1_1_, author0_.name as name1_1_,
location1_.id as id0_0_, location1_.version as version0_0_, location1_.city
as city0_0_ from author author0_ inner join location location1_ on
author0_.location_id=location1_.id where author0_.id=?

location1
location1
location1
location2
location3


I don't understand why the second time that I run the script Hibernate
accesses only one time to the database, and why the third and next times
that I run the script Hiberante accesses 5 times to the database. 

Can somebody explain me this behavior?

Thank you very much!!


--
View this message in context: http://grails.1312388.n4.nabble.com/GORM-Gotchas-Being-eager-tp4454823p4454823.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM Gotchas - Being eager

jl_monteagudo
Hello Rodrigo,

I have seen your issue in JIRA, and yes, is the same problem that you have reported.

It is very rare that the first query works fine and the next ones don't.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM Gotchas - Being eager

rosenfeld
Em 08-03-2012 06:38, jl_monteagudo escreveu:
> Hello Rodrigo,
>
> I have seen your issue in JIRA, and yes, is the same problem that you have
> reported.
>
> It is very rare that the first query works fine and the next ones don't.

Indeed.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM Gotchas - Being eager

jl_monteagudo
In reply to this post by jl_monteagudo
I have tested the same domain and the same script with Grails 1.3.7 version. With this version the fetch:join mapping works fine. All the times that I run the script I get the following SQL log:

Hibernate:
    select
        count(*) as y0_
    from
        author this_
Hibernate:
    select
        this_.id as id2_1_,
        this_.version as version2_1_,
        this_.location_id as location3_2_1_,
        this_.name as name2_1_,
        location2_.id as id1_0_,
        location2_.version as version1_0_,
        location2_.city as city1_0_
    from
        author this_
    inner join
        location location2_
            on this_.location_id=location2_.id
location1
location1
location1
location2
location3

There is only one access to the database. Ok, really there are two accesses, the first one is a "count" select, but I don't understand why this "count" query is done.

So, I think that the Grails 2.0 behavior is not right  
Loading...