I have a mapped object A with a foreign key to mapped object B.
I need to display the information (A+B) in a table. The not so smart way
is to first query on the As and then for each A, get the B. This
results in n+1 selects.
Is it possible to ask Mapper, when querying for the As, that it should
join with Bs table to fetch the B at the same time (ie the equivalent of
join fetching in Hibernate)?
/Jeppe
> You should be able to use the PreCache QueryParam to tell Mapper to
> pre-fetch the mapped object:
>
> http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/mapper/PreCache.html
Ahh nice. I missed that one, Thanks
/Jeppe
Ok, time for a bonus question: Can this be extended to more than 1 level?
I have something similar to the following
class Vehicle extends LongKeyedMapper[Vehicle] with IdPK {
object vehicleType extends MappedLongForeignKey(this, VehicleType)
}
class VehicleType extends LongKeyedMapper[VehicleType] with IdPK {
object brand extends MappedLongForeignKey(this, Brand)
}
class Brand extends LongKeyedMapper[Brand] with IdPK {
}
When displaying the list of vehicles I need to show both the vehicle,
the vehicleType & the brand in the same table
So to get the list of vehicles and caching the vehicletypes I do this:
override def findForListParams: List[QueryParam[Vehicle]] =
List(By(account, User.currentUser.open_!.account.is),
PreCache(vehicleType))
But I still get the n+1 selects when trying to resolve
vehicleType.brand. Can this be remedied somehow?
/Jeppe
Ok, time for a bonus question: Can this be extended to more than 1 level?
On Thu, Aug 13, 2009 at 5:12 PM, Derek Chen-Becker<dchen...@gmail.com> wrote:
> You should be able to use the PreCache QueryParam to tell Mapper to
> pre-fetch the mapped object:
>
> http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/mapper/PreCache.html
I have something similar to the following
class Vehicle extends LongKeyedMapper[Vehicle] with IdPK {
object vehicleType extends MappedLongForeignKey(this, VehicleType)
}
class VehicleType extends LongKeyedMapper[VehicleType] with IdPK {
object brand extends MappedLongForeignKey(this, Brand)
}
class Brand extends LongKeyedMapper[Brand] with IdPK {
}
When displaying the list of vehicles I need to show both the vehicle,
the vehicleType & the brand in the same table
So to get the list of vehicles and caching the vehicletypes I do this:
override def findForListParams: List[QueryParam[Vehicle]] =
List(By(account, User.currentUser.open_!.account.is),
PreCache(vehicleType))
But I still get the n+1 selects when trying to resolve
vehicleType.brand. Can this be remedied somehow?
/Jeppe
Believe me, I tried :-) Couldn't really figure out what to use as
parameter and looking at dealWithPrecache it seems like the two tables
need to be directly linked
It's not super critial atm, so I'll get back to this if necessary
/Jeppe