Quantcast

There is no findById method?

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

There is no findById method?

tomcatlee
I triy to use  SomeDomain.findById(1)
but failed.
So how to get instance by id property?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: There is no findById method?

Mike Sickler
SomeDomain.get(1)

On Fri, Nov 6, 2009 at 11:39 PM, tomLee <[hidden email]> wrote:

I triy to use  SomeDomain.findById(1)
but failed.
So how to get instance by id property?
--
View this message in context: http://old.nabble.com/There-is-no-findById-method--tp26235470p26235470.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
star

Re: There is no findById method?

burtbeckwith
findById should work, but it's best to use get() since it has the same result and willl use the 2nd-level cache if configured.

Burt

> SomeDomain.get(1)
>
> On Fri, Nov 6, 2009 at 11:39 PM, tomLee <[hidden email]> wrote:
>
> >
> > I triy to use  SomeDomain.findById(1)
> > but failed.
> > So how to get instance by id property?
> > --
> > View this message in context:
> > http://old.nabble.com/There-is-no-findById-method--tp26235470p26235470.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
> >
> >
> >
>

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: There is no findById method?

Robert Fletcher
You can also use read(id) in place of get(id) to get a read-only instance.

---------------------------------------------------------------------
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
star

Re: There is no findById method?

Glenn Saqui-3
Burt,

I thought the difference between the get(id) and the findById was that the get didn't use the second level cache if configured and the findById would use the second level cache.  Have I got this backwards?

Glenn

On Sat, Nov 7, 2009 at 11:12 AM, Robert Fletcher <[hidden email]> wrote:
You can also use read(id) in place of get(id) to get a read-only instance.

---------------------------------------------------------------------
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
star

Re: There is no findById method?

burtbeckwith
Yep. get() (and read(), which uses get() and sets the instance to read-only) is the only method that always uses the 2nd-level cache. load() will too but it's not mapped in GORM. All other methods are variants of criteria queries or HQL queries that support using the query cache (which is separate from the instance cache that get() uses) but don't use it by default.

This is easy to test. Turn on basic sql logging in DataSource.groovy:

   dataSource {
      pooled = true
      driverClassName = ...
      ...
      loggingSql = true
   }

and create a simple cached class:

   class Thing {
      String name
      static mapping = {
         cache true
      }
   }

Run "grails console" and create an instance:

   def thing = new Thing(name: 'foo').save()

then load it using findById() and note that repeated calls generate SQL each time:

   println Thing.findById(1L).name

and load it using get() and note that only the 1st invocation generates SQL and repeated calls don't:

   println Thing.get(1L).name

and you can then call findById() again and it still hits the database each time even though that instance is cached.

Burt

> Burt,
>
> I thought the difference between the get(id) and the findById was that the
> get didn't use the second level cache if configured and the findById would
> use the second level cache.  Have I got this backwards?
>
> Glenn
>
> On Sat, Nov 7, 2009 at 11:12 AM, Robert Fletcher <
> [hidden email]> wrote:
>
> > You can also use read(id) in place of get(id) to get a read-only instance.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: There is no findById method?

tomcatlee
I found I can't use read/get/findById on Account domain,I am very curious about this, please check my code:

class Account {
        double amount
        String freeTime
        String type
        User upUser
        static hasMany = [introduction:User]
        static belongsTo = User
    static constraints = {
                type(nullable:true)
                amount(nullable:true)
                introduction(nullable:true)
                freeTime(nullable:true)
                upUser(nullable:true)
    }
}

class User {
        Account act
        .......
   }

SQL log:
Hibernate:
    select
        account0_.id as id8_0_,
        account0_.version as version8_0_,
        account0_.amount as amount8_0_,
        account0_.free_time as free4_8_0_,
        account0_.type as type8_0_
    from
        account account0_
    where
        account0_.id=?

I query account in gsp file:
..
if(user.actId) {
          //def act=Account.get(3)
         def act=Account.read(3) // even I use constant 3,it's not working
...

I have done the following:
delete the project in user profile folder, run grails clean and grails compile.

Please help!


































Burt Beckwith wrote
Yep. get() (and read(), which uses get() and sets the instance to read-only) is the only method that always uses the 2nd-level cache. load() will too but it's not mapped in GORM. All other methods are variants of criteria queries or HQL queries that support using the query cache (which is separate from the instance cache that get() uses) but don't use it by default.

This is easy to test. Turn on basic sql logging in DataSource.groovy:

   dataSource {
      pooled = true
      driverClassName = ...
      ...
      loggingSql = true
   }

and create a simple cached class:

   class Thing {
      String name
      static mapping = {
         cache true
      }
   }

Run "grails console" and create an instance:

   def thing = new Thing(name: 'foo').save()

then load it using findById() and note that repeated calls generate SQL each time:

   println Thing.findById(1L).name

and load it using get() and note that only the 1st invocation generates SQL and repeated calls don't:

   println Thing.get(1L).name

and you can then call findById() again and it still hits the database each time even though that instance is cached.

Burt

> Burt,
>
> I thought the difference between the get(id) and the findById was that the
> get didn't use the second level cache if configured and the findById would
> use the second level cache.  Have I got this backwards?
>
> Glenn
>
> On Sat, Nov 7, 2009 at 11:12 AM, Robert Fletcher <
> robert.w.fletcher@googlemail.com> wrote:
>
> > You can also use read(id) in place of get(id) to get a read-only instance.
> >
> > ---------------------------------------------------------------------
> > 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
star

Re: There is no findById method?

Florian Freudenberg
I think you cannot access the domain class from the GSP. Such things
like Account.get(3) have to be done in the controller which return it
to the GSP.

On Sun, Nov 8, 2009 at 5:10 AM, tomLee <[hidden email]> wrote:

>
> I found I can't us read/get/findById on Account domain,I am very curious
> about this, please check my code:
>
> class Account {
>        double amount
>        String freeTime
>        String type
>        User upUser
>        static hasMany = [introduction:User]
>        static belongsTo = User
>    static constraints = {
>                type(nullable:true)
>                amount(nullable:true)
>                introduction(nullable:true)
>                freeTime(nullable:true)
>                upUser(nullable:true)
>    }
> }
>
> class User {
>        Account act
>        .......
>   }
>
> SQL log:
> Hibernate:
>    select
>        account0_.id as id8_0_,
>        account0_.version as version8_0_,
>        account0_.amount as amount8_0_,
>        account0_.free_time as free4_8_0_,
>        account0_.type as type8_0_
>    from
>        account account0_
>    where
>        account0_.id=?
>
> I query account in gsp file:
> ..
> if(user.actId) {
>          //def act=Account.get(3)
>         def act=Account.read(3) // even I use constant 3,it's not working
> ...
>
> I have done the following:
> delete the project in user profile folder, run grails clean and grails
> compile.
>
> Please help!
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Burt Beckwith wrote:
>>
>> Yep. get() (and read(), which uses get() and sets the instance to
>> read-only) is the only method that always uses the 2nd-level cache. load()
>> will too but it's not mapped in GORM. All other methods are variants of
>> criteria queries or HQL queries that support using the query cache (which
>> is separate from the instance cache that get() uses) but don't use it by
>> default.
>>
>> This is easy to test. Turn on basic sql logging in DataSource.groovy:
>>
>>    dataSource {
>>       pooled = true
>>       driverClassName = ...
>>       ...
>>       loggingSql = true
>>    }
>>
>> and create a simple cached class:
>>
>>    class Thing {
>>       String name
>>       static mapping = {
>>          cache true
>>       }
>>    }
>>
>> Run "grails console" and create an instance:
>>
>>    def thing = new Thing(name: 'foo').save()
>>
>> then load it using findById() and note that repeated calls generate SQL
>> each time:
>>
>>    println Thing.findById(1L).name
>>
>> and load it using get() and note that only the 1st invocation generates
>> SQL and repeated calls don't:
>>
>>    println Thing.get(1L).name
>>
>> and you can then call findById() again and it still hits the database each
>> time even though that instance is cached.
>>
>> Burt
>>
>>> Burt,
>>>
>>> I thought the difference between the get(id) and the findById was that
>>> the
>>> get didn't use the second level cache if configured and the findById
>>> would
>>> use the second level cache.  Have I got this backwards?
>>>
>>> Glenn
>>>
>>> On Sat, Nov 7, 2009 at 11:12 AM, Robert Fletcher <
>>> [hidden email]> wrote:
>>>
>>> > You can also use read(id) in place of get(id) to get a read-only
>>> instance.
>>> >
>>> > ---------------------------------------------------------------------
>>> > To unsubscribe from this list, please visit:
>>> >
>>> >    http://xircles.codehaus.org/manage_email
>>> >
>>> >
>>> >
>>>
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/There-is-no-findById-method--tp26235470p26250879.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
>
>
>

---------------------------------------------------------------------
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
star

Re: There is no findById method?

tomcatlee
I deleted  static hasMany = [introduction:User] ,now it's working
Florian Freudenberg wrote
I think you cannot access the domain class from the GSP. Such things
like Account.get(3) have to be done in the controller which return it
to the GSP.

On Sun, Nov 8, 2009 at 5:10 AM, tomLee <tomcatleee@gmail.com> wrote:
>
> I found I can't us read/get/findById on Account domain,I am very curious
> about this, please check my code:
>
> class Account {
>        double amount
>        String freeTime
>        String type
>        User upUser
>        static hasMany = [introduction:User]
>        static belongsTo = User
>    static constraints = {
>                type(nullable:true)
>                amount(nullable:true)
>                introduction(nullable:true)
>                freeTime(nullable:true)
>                upUser(nullable:true)
>    }
> }
>
> class User {
>        Account act
>        .......
>   }
>
> SQL log:
> Hibernate:
>    select
>        account0_.id as id8_0_,
>        account0_.version as version8_0_,
>        account0_.amount as amount8_0_,
>        account0_.free_time as free4_8_0_,
>        account0_.type as type8_0_
>    from
>        account account0_
>    where
>        account0_.id=?
>
> I query account in gsp file:
> ..
> if(user.actId) {
>          //def act=Account.get(3)
>         def act=Account.read(3) // even I use constant 3,it's not working
> ...
>
> I have done the following:
> delete the project in user profile folder, run grails clean and grails
> compile.
>
> Please help!
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Burt Beckwith wrote:
>>
>> Yep. get() (and read(), which uses get() and sets the instance to
>> read-only) is the only method that always uses the 2nd-level cache. load()
>> will too but it's not mapped in GORM. All other methods are variants of
>> criteria queries or HQL queries that support using the query cache (which
>> is separate from the instance cache that get() uses) but don't use it by
>> default.
>>
>> This is easy to test. Turn on basic sql logging in DataSource.groovy:
>>
>>    dataSource {
>>       pooled = true
>>       driverClassName = ...
>>       ...
>>       loggingSql = true
>>    }
>>
>> and create a simple cached class:
>>
>>    class Thing {
>>       String name
>>       static mapping = {
>>          cache true
>>       }
>>    }
>>
>> Run "grails console" and create an instance:
>>
>>    def thing = new Thing(name: 'foo').save()
>>
>> then load it using findById() and note that repeated calls generate SQL
>> each time:
>>
>>    println Thing.findById(1L).name
>>
>> and load it using get() and note that only the 1st invocation generates
>> SQL and repeated calls don't:
>>
>>    println Thing.get(1L).name
>>
>> and you can then call findById() again and it still hits the database each
>> time even though that instance is cached.
>>
>> Burt
>>
>>> Burt,
>>>
>>> I thought the difference between the get(id) and the findById was that
>>> the
>>> get didn't use the second level cache if configured and the findById
>>> would
>>> use the second level cache.  Have I got this backwards?
>>>
>>> Glenn
>>>
>>> On Sat, Nov 7, 2009 at 11:12 AM, Robert Fletcher <
>>> robert.w.fletcher@googlemail.com> wrote:
>>>
>>> > You can also use read(id) in place of get(id) to get a read-only
>>> instance.
>>> >
>>> > ---------------------------------------------------------------------
>>> > To unsubscribe from this list, please visit:
>>> >
>>> >    http://xircles.codehaus.org/manage_email
>>> >
>>> >
>>> >
>>>
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/There-is-no-findById-method--tp26235470p26250879.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
>
>
>

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

    http://xircles.codehaus.org/manage_email

Loading...