PreCache works as expected.

Here's the data model:

class RoomChoice extends LongKeyedMapper[RoomChoice] with IdPK {
  def getSingleton = RoomChoice
  object name extends MappedPoliteString(this, 512)
  object price extends MappedInt(this){
    override def defaultValue = 0
  }
}

object RoomChoice extends RoomChoice with LongKeyedMetaMapper[RoomChoice]


class User extends LongKeyedMapper[User] with IdPK {
  def getSingleton = User
  object account extends MappedPoliteString(this, 10)
  object roomChoice extends MappedLongForeignKey(this, RoomChoice)
}

object User extends User with LongKeyedMetaMapper[User]

And here's the test:


    for {
      i <- 1 to 50
    } {
    User.create.roomChoice(RoomChoice.create.name("Fog "+i).saveMe).save
    }


    User.findAll(PreCache(User.roomChoice)).foreach{
      u => if (!u.roomChoice.cached_?) error("Failed "+u)
    }

    User.findAll().foreach{
      u => if (u.roomChoice.cached_?) error("Failed "+u)
    }


    val rooms =
User.findAll(PreCache(User.roomChoice)).flatMap(_.roomChoice.obj)
    println("Rooms "+rooms)

I'm enclosing code the demonstrate the functionality.  Please let us know if
there's something missing.

Thanks,

David

On Mon, Aug 24, 2009 at 9:16 PM, koji <[email protected]> wrote:

>
> Oops, so sorry for pasting wrong part.
> And sorry i didnt show my model's code
> my code is as below
>
> class RoomChoice extends LongKeyedMapper[RoomChoice] with IdPK {
>     def getSingleton = RoomChoice
>     object travel extends MappedLongForeignKey(this, Travel)
>     object name extends MappedPoliteString(this, 512)
>     object price extends MappedInt(this){
>        override def defaultValue = 0
>     }
> }
> class User extends LongKeyedMapper[User] with IdPK {
>     def getSingleton = User
>     object travel extends MappedLongForeignKey(this, Travel)
>     object account extends MappedPoliteString(this, 10)
>     object roomChoice extends MappedLongForeignKey(this,
> RoomChoice)
> }
>
> val users = User.findAll(By(User.travel, travelId),PreCache
> (User.roomChoice), OrderBy(User.id, Ascending))
>
> thank you
>
> On Aug 25, 6:40 am, Derek Chen-Becker <[email protected]> wrote:
> > Maybe I'm missing something, but you're telling Mapper to pre-cache
> > User.roomChoice, but then you're accessing User.fromPlace. Are those two
> > fields related somehow?
> >
> > Derek
> >
> > On Mon, Aug 24, 2009 at 12:25 AM, koji <[email protected]> wrote:
> >
> > > Hi all,
> > > I want to avoid n+1 problem
> > > so i try to write my code as
> > > val users = User.findAll(PreCache(User.roomChoice), By(User.travel,
> > > travelId))
> > > and i saw the sql generated only once for querying roomChoice.
> > > but when i using the
> > > u.fromPlace.obj, it always become Empty(u.fromPace has correct value)
> >
> > > thanks
>
> >
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Attachment: precache.tgz
Description: GNU Zip compressed data

Reply via email to