users@glassfish.java.net

Re: FetchType.EAGER behaviour

From: <glassfish_at_javadesktop.org>
Date: Thu, 12 Jul 2007 01:35:56 PDT

This is more exactly what I have (using cnnordins terminology):

entity Level1
Collection<Level2> level2s (oneToMany)
String name;

entity Level2
Collection<Level3> level3s (oneToMany)
Level1 parent (manyToOne)
String name;

entity Level3
Level2 parent (manyToOne)
String name;

Lets say I have the following in the database tables:
Level1: id,name
1, "myLevel1"

Level2: id,parent_id references Level1(id),name
1, 1, "myFirstLevel2"
2, 1, "mySecondLevel2"

Level3: id,parent_id references Level2(id),name
1, 1, "myFirstLevel3"
2, 1, "mySecondLevel3"
3, 2 "myThirdLevel3"
4, 2, "myFourthLevel3"

All children collections are set to fetchtype.eager. If everything worked as I thought it would, toplink would produce an sql looking something like this, when getting the Level1 (id=1) object:
select "*" from level1 t1 join level2 t2 on t1.id=t2.parent_id join level3 t3 on t3.id=t2.parent_id where t1.id=1
resulting in:
1, "myLevel1",1, 1, "myFirstLevel2",1, 1, "myFirstLevel3"
1, "myLevel1",1, 1, "myFirstLevel2",2, 1, "mySecondLevel3"
1, "myLevel1",2, 1, "mySecondLevel2",3, 2 "myThirdLevel3"
1, "myLevel1",2, 1, "mySecondLevel2",4, 2, "myFourthLevel3"
and from that build the level1 object with all its children.

However, it doesn't. It simply makes one select for level1 and then one for each level2 object and then one for each level3 object. The same as if I use lazy where the childobjects will be fetched when accessing them.

With the fetch join I can get Level1 with its child objects level2, but not deeper. Toplink will fetch the level3 children of level2 with one select each.

If this is working as intended, how are one supposed to create tree structures with toplink without having to wait minutes for all the selects to be ready?
[Message sent by forum member 'danielcroth' (danielcroth)]

http://forums.java.net/jive/thread.jspa?messageID=226294