You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Sebastian Thomschke <se...@web.de> on 2003/05/11 23:58:02 UTC

Criteria.copy() method

Hi,

I just realized, that the copy method of Criteria class doesn't really 
do what - at least I - expected.
First not all properties are copied (m_type,m_embraced, m_alias, 
m_query, m_parentCriteria) and second the property m_criteria of the new 
copy references the same vector as the original criteria! So when I add 
some conditions to the original criteria object, they also become 
effective for the copied criteria and vice versa.
Shouldn't instead the m_criteria.clone() method be used?

I mean if it was the real intention that the copied object contains the 
same references like the orginal, why should one ever create such a copy 
and not directly work with the orginal?

Example:
---------

public Criteria copy(boolean includeGroupBy, boolean includeOrderBy, 
boolean includePrefetchedRelationships)
{
    Criteria copy = new Criteria();

    copy.m_criteria = (Vector) m_criteria.clone();
    if (includeGroupBy)
    {
        copy.groupby = (ArrayList) ((ArrayList) groupby).clone();
    }
    if (includeOrderBy)
    {
        copy.orderby =  (ArrayList) ((ArrayList) orderby).clone();
    }
    if (includePrefetchedRelationships)
    {
        copy.prefetchedRelationships =  (ArrayList) ((ArrayList) 
prefetchedRelationships).clone();
    }
    return copy;
}

Thanks,
Sebastian

How about adding a clone() method to the Criteria class?


Re: Criteria.copy() method

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi sebastien,

this method is only needed to create the count query based on the 
criteria. maybe the name is misleading.

jakob

Sebastian Thomschke wrote:

> Hi,
>
> I just realized, that the copy method of Criteria class doesn't really 
> do what - at least I - expected.
> First not all properties are copied (m_type,m_embraced, m_alias, 
> m_query, m_parentCriteria) and second the property m_criteria of the 
> new copy references the same vector as the original criteria! So when 
> I add some conditions to the original criteria object, they also 
> become effective for the copied criteria and vice versa.
> Shouldn't instead the m_criteria.clone() method be used?
>
> I mean if it was the real intention that the copied object contains 
> the same references like the orginal, why should one ever create such 
> a copy and not directly work with the orginal?
>
> Example:
> ---------
>
> public Criteria copy(boolean includeGroupBy, boolean includeOrderBy, 
> boolean includePrefetchedRelationships)
> {
>    Criteria copy = new Criteria();
>
>    copy.m_criteria = (Vector) m_criteria.clone();
>    if (includeGroupBy)
>    {
>        copy.groupby = (ArrayList) ((ArrayList) groupby).clone();
>    }
>    if (includeOrderBy)
>    {
>        copy.orderby =  (ArrayList) ((ArrayList) orderby).clone();
>    }
>    if (includePrefetchedRelationships)
>    {
>        copy.prefetchedRelationships =  (ArrayList) ((ArrayList) 
> prefetchedRelationships).clone();
>    }
>    return copy;
> }
>
> Thanks,
> Sebastian
>
> How about adding a clone() method to the Criteria class?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>