You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Marc Lustig <ma...@marclustig.com> on 2003/01/14 21:19:08 UTC

problem retrieving foreign collection

Hi,

I have a strange behaviour here:
There is one Projekt related to my user.

user.getProjekts(); returns zero Projekts (WRONG)

When I replace the line with
user.getProjekts(new Criteria());
one Projekt is returned (correct).

I really can't follow what is going on here, as method getProjekts() simply
creates an empty Criteria and passes to getProjekts(Criteria c):

    public List getProjekts() throws TorqueException
    {
        if (collProjekts == null)
        {
            collProjekts = getProjekts(new Criteria(10));
        }
        return collProjekts;
    }

What's that?

Marc


Re: problem retrieving foreign collection

Posted by "Peter S. Hamlen" <ph...@mail.com>.
The "getProjekts" behavior is slightly complicated.  If you look at the
generated code, you'll see that it does a form of "lazy" loading. 
Essentially, it only loads the projects if they haven't been loaded
already.

So you could get your behavior in the following scenario:
1)  Create/load a user from the database.
2)  Create a criteria that will not return any projekts 
3)  Call getProjekts(funkyCriteria) - this will return 0 from the
database because no projekts meeet the criteria.
4)  Call getProjekts() - this will STILL return 0 because projekts have
already been loaded.

You could also get your behavior by the following:
1)  Create/load a user from the database.
2)  Call getProjekts() - this will return 0 from the database because no
records have been added yet.
3)  Create a projekt for the user by setting the UserId for the projekt
and save it to the database.
4)  Call get Projekts() - it will still return 0 because THE PROJEKTS
HAVE ALREADY BEEN LOADED.
5)  The workaround for this problem is to call user.addProjekt() BEFORE
you save the projekt to the database.  addProjekt will add the new
projekt to the collection.

Hope this helps
-Peter

On Tue, 2003-01-14 at 15:19, Marc Lustig wrote:
> Hi,
> 
> I have a strange behaviour here:
> There is one Projekt related to my user.
> 
> user.getProjekts(); returns zero Projekts (WRONG)
> 
> When I replace the line with
> user.getProjekts(new Criteria());
> one Projekt is returned (correct).
> 
> I really can't follow what is going on here, as method getProjekts() simply
> creates an empty Criteria and passes to getProjekts(Criteria c):
> 
>     public List getProjekts() throws TorqueException
>     {
>         if (collProjekts == null)
>         {
>             collProjekts = getProjekts(new Criteria(10));
>         }
>         return collProjekts;
>     }
> 
> What's that?
> 
> Marc
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>