db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Fischer <tfisc...@apache.org>
Subject AW: Joins in torque
Date Sat, 21 Jan 2006 07:24:37 GMT
Hi,

I do not understand your problem. My guess is that the misunderstanding 
arises a) because of Torque's silent db hits and caching the associated 
objects and b) some peculiarities of the caching.
Note: All this assumes you have standard generator settings (no 
managers etc).

regarding a) Assume you have the following code: (All this assumes you 
have standard generator settings (no managers etc)).

Criteria criteria = new Criteria();
List t12s = T12Peer.doSelect(criteria);
T12 t12 = (T12) T12s.get(0);
T1 t1 = t12.getT1(); // will query the db. t12.t1 was not read before.
T1 othert1 = t12.getT1(); // Will not query db. t12.t1 Was read before.
                           // (in fact, t1 == othert1)

And now using doSelectJoinXXX

Criteria criteria = new Criteria();
List t12s = T12Peer.doSelectJoinT1(criteria);
T12 t12 = (T12) T12s.get(0);
T1 t1 = t12.getT1(); // will NOT query the db. t12.t1 was already read
                      // by the doSelectJoin method

And finally (to complete the confusion):

Criteria criteria = new Criteria();
criteria.addJoin(T12Peer.T1_ID,T1Peer.T1_ID);
List t12s = T12Peer.doSelect(criteria);
T12 t12 = (T12) T12s.get(0);
T1 t1 = t12.getT1(); // will query the db! explanation below.

The first two examples should be clear because once you do T12.getT1() 
the t1 result is cached in t12 and not read again. The DoSelectJoinXXX 
method fills the t12.t1 objects by using a join.

The last example is more difficult: If you use Criteria.addJoin(), the 
join is NOT used to fill the associated objects (in this case t12.t1). The 
join can only be used to specify which t12's can be read.

regarding b) Consider the following code:
Criteria criteria = new Criteria();
List t12s = T12Peer.doSelect(criteria);
T12 t12 = (T12) T12s.get(0);
T1 t1 = t12.getT1();
List associatedT12s = t1.getT12s(); // will hit db! was not filled before

Even if ((T12)associatedT12s.get(0)).equals(t12) would be true, in no case 
will ((T12)associatedT12s.get(0)) == t12 will be true. Just because you 
fill one "direction" of association, this does not mean the other 
"direction" is filled automatically. Read and debug the generated source 
code of the methods T12.getT1() and t1.getT12s() for more information.

I hope this shed some light on what is going on.

      Thomas


On Fri, 20 Jan 2006, Stephan Spiegel wrote:

> Hi,
>
> first thanks for the fast and good response.
>
> The doSelectJoinXXX methods are in the BaseT12Peer because here are the
> foreign keys pointing to T1 and T2, so I could/can do T12.doSelectJoinT1()
> and T12.doSelectJoinT2(). The point is, that I do not get any other result
> then just doing a doSelect and manually bring them together as done in the
> following example:
>
> (Method in T12Peer):
>
> public static List bringThemTogether () throws TorqueException {
> 	Criteria crit = new Criteria();
> 	List xx = doSelect(crit);
> 	for (int i = 0; i < xx.size(); i++) {
> 		T12 temp_obj1 = (T12)xx.get(i);
> 		T1 temp_obj2 = (T1)temp_obj1.getT1();
> 		T2 temp_obj3 = (T2)temp_obj1.getT2();
> 		HashMap h = new HashMap();
> 		h.put("Table1",temp_obj2);
> 		h.put("Table2",temp_obj3);}
> 		xx.set(i,h);
> 	}
> 	return xx;
> }
>
> using this I can directly access the relations between the values of T1 and
> T2.
>
> I can't really see an effect using the doSelectJoin methods, but may be I'm
> totally blind in the moment. Or does the above method hundreds of selects?
> (I will have a look for the log4j as mentioned of Greg)
>
> Stephan
>
> -----Ursprüngliche Nachricht-----
> Von: Thomas Fischer [mailto:fischer@seitenbau.net]
> Gesendet: Donnerstag, 19. Januar 2006 17:57
> An: Apache Torque Users List
> Betreff: RE: Joins in torque
>
>
>
>
>
>
> Hi,
>
> There is no built-in support at the moment to get more than one level of
> indrection read in at once.
> As far as I can see, you have 3 1/2 possibilities:
>
> 1a) read the values one after another. Assuming you have defined foreign
> keys (I'm not sure whether they should defined in T1 and T2 or in T12, on
> this depends whether the doSelectJoinXXX methods are generated in T1Peer
> and T2Peer or in T12Peer), do T1Peer.doSelectJoinT12(). For each T1Entry
> you get, fetch the corresponding T12 Entry (this is already read from the
> database) and then do T12.getT2s() (this will access the database each time
> you fetch a T2 Entry).
>
> 1b) do the same as in a1, but omit the T12.getT2s() step. Instead, get all
> T2-Ids from all T12 you read in, do T2Peer.doSelect() with a criteria where
> all needed T2Ids are selected, and do the linking of the T2 objects to the
> T12's manually.
>
> 2) look at the template code for generating the doSelectJoinXXX methods and
> extend them for two levels of indirection.
>
> 3) A time ago, someone proposed a class which can read a whole tree of
> objects at once. It can be found in scarab (somewhere around TRQS260). It
> is rather reflection-centric and I do not have an idea whether it works
> with Torque 3.2, but maybe that is what you want.
>
> Personally, I use a modified method 1b). I would read all T1 I want, gather
> the collection of T1 ids to read the associated T12's and link them
> manually to the T1's. Then I'd use the T2 ids from there to read the T2's,
> and link them manually to the T12's. This is rather fast (one select per
> level of indirection, no object is transferred twice) and quite easy to
> implement. You might have to override the initCollXXX() methods for the
> used collections to make them public.
>
>    Thomas
>
>
> "Stephan Spiegel" <spiegel@szwo.de> schrieb am 19.01.2006 17:17:29:
>
>> Hi, (in case this message is double please excuse me)
>>
>> I really do not understand what to do, I do not get the expected results!
>>
>> Let´s say I have 3 Tables: T1, T2 and T1_2
>> T1:
>> ID (integer pk)
>> value (varchar)
>> ...
>>
>> T2:
>> ID (integer pk)
>> value (varchar)
>> ...
>>
>> T1_2:
>> T1_ID (integer fk->T1)
>> T2_ID (integer fk->T2)
>>
>> As a result, I would like to have a union table showing the values of T1
> and
>> T2
>> in SQL this would be: select T1.value, T2.value from T1, T2, T1_2 where
>> T1.ID = T1_2.T1_ID and T2.ID = T1_2.T2_ID
>>
>> if I get more than this (select * ...) would be fine as well.
>>
>> So, I have my classes but when I try to use them as written in Peers
> HowTo
>> (doSelectJoin...) or in Criteria HowTo (crit.addJoin(...)) I always get
> only
>> the values of the T1_2-table
>>
>> As a first step, I try just to get the first half (result should show:
>> T1.value, T1_2.T2_ID)
>> so it should correspond to:
>> doSelectJoinT1(crit) in T1_2Peer
>> or:
>> crit.addJoin(T1.ID,T1_2.T1_ID,Criteria.LEFT_JOIN)
>>
>> as a result I just get the T1_2 values. I checked the
> mailing-list-archives,
>> I can´t find a helpful message. Where is my mistake? Could somebody tell
> me
>> exactly how to add this join?
>>
>> (I´m using torque-3.2, MySQL 5, defaultidMethod=native)
>>
>> thanks in advance, Stephan
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
>> For additional commands, e-mail: torque-user-help@db.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>
>

Mime
View raw message