-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Single Query for children of N objects of the same type
PostPosted: Mon Jul 25, 2005 3:23 am 
Newbie

Joined: Mon Jul 25, 2005 1:38 am
Posts: 2
Hi All,

Just wondering if there is a more efficient way to create many to many mappings than i'm currently using?

For example, I have setup 2 objects, Car and Driver.
Car has Id, LicensePlate and a list of the Driver ids.
Driver has Id, Name and a list of the Car Ids.

I have not linked the objects as i do not want any automatic loading at all.

This is the mapping:


Code:
   <class name="TestSecurity.Car, TestSecurity" table="Car">
      <id name="Id" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <property name="LicensePlate" />
      <set name="DriverIds" table="CarDriver">
         <key column="CarId" />
         <element column="DriverId" type="Int32" />
      </set>
   </class>
   
   <class name="TestSecurity.Driver, TestSecurity" table="Driver">
      <id name="Id" type="Int32" unsaved-value="0">
         <generator class="native" />
      </id>
      <property name="Name" />
      <set name="CarIds" table="CarDriver">
         <key column="DriverId" />
         <element column="CarId" type="Int32" />
      </set>
   </class>




And when i execute


Code:
Find("From Car c Where c.Id IN (1,2,3,4)");




I can see in the sql trace that 5 sql statements have been executed, 1 to retrieve the list of cars, and another for each car to retrieve the drivers.

Code:
select car0_.Id as Id, car0_.LicensePlate as LicenseP2_ from Car car0_ where (car0_.Id IN(1 , 2 , 3 , 4))

exec sp_executesql N'SELECT cardrive0_.CarId as CarId__, cardrive0_.DriverId as DriverId__ FROM CarDriver cardrive0_ WHERE cardrive0_.CarId = @p0', N'@p0 int', @p0 = 4

exec sp_executesql N'SELECT cardrive0_.CarId as CarId__, cardrive0_.DriverId as DriverId__ FROM CarDriver cardrive0_ WHERE cardrive0_.CarId = @p0', N'@p0 int', @p0 = 3

exec sp_executesql N'SELECT cardrive0_.CarId as CarId__, cardrive0_.DriverId as DriverId__ FROM CarDriver cardrive0_ WHERE cardrive0_.CarId = @p0', N'@p0 int', @p0 = 2

exec sp_executesql N'SELECT cardrive0_.CarId as CarId__, cardrive0_.DriverId as DriverId__ FROM CarDriver cardrive0_ WHERE cardrive0_.CarId = @p0', N'@p0 int', @p0 = 1



If i were to do this by hand, i would probably do 2 queries, one to retrieve the cars, another to retrieve all the car drivers, such as

Code:
SELECT Id, LicensePlate FROM Car WHERE (Id IN(1, 2, 3, 4))

SELECT CarId, DriverId FROM CarDriver WHERE (CarId IN (1, 2, 3, 4)



Is there any way to make hibernate do this, ie, get the collection items for the car (in this case the driver list) in a single query for multiple items? Is there a better mapping strategy that would reduce the number of queries needed?

Cheers -Mike


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 4:26 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Quote:
SELECT CarId, DriverId FROM CarDriver WHERE (CarId IN (1, 2, 3, 4)

I think the way you suggested to do it by hand would get you a list of CarIds and DriverIds, but you could not assign all cars to a certain driver.
The relation between car and driver would get lost resp. messed up.

Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 5:28 am 
Newbie

Joined: Mon Jul 25, 2005 1:38 am
Posts: 2
sven wrote:
I think the way you suggested to do it by hand would get you a list of CarIds and DriverIds, but you could not assign all cars to a certain driver.
The relation between car and driver would get lost resp. messed up.

Best regards
Sven


Hi Sven,

I'm sorry, I did not mention that this in the earlier post, but the CarDriver table is a many to many joining table keyed off CarId and DriverId.

From some sample data:

-First sql proc gets car data.
Code:
Car
Id      License Plate
1   ABC120
2   ABC121
3   ABC122
4   ABC123



-Second sql proc gets car driver data.
Code:
CarDriver
CarId DriverId
1   1
2   1
2   2
2   3
3   3
4   3



-The Application could then go through second set of data and adds the driverid to the appropriate object based on the carid field. Can this be done in hibernate?


Cheers -Mike


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.