I think there is a valid approach to retrieving records based on a criteria
object that is of the same class as the object you are retrieving.
A Record object may be used as the criteria to find other Record objects.
If we have a method such as
Record[] find(Record record);
then the argument can have any of its fields as null
this would signify no filtering on that field.
A criteria object could be set up as so:
public Record(Integer key, String[] data);
new Record(null, new String {"Foo", "Bar", null, null, null, null, null})
I think Hibernate uses this approach.
But I'm a little unhappy about this, a null primary key?
Is this abuse of the object? should I have a separate criteria class?
I'm after the simplest design that I do feel happy about.

Signature
Mike W
Chris Uppal - 24 Aug 2004 09:08 GMT
> I think there is a valid approach to retrieving records based on a
> criteria object that is of the same class as the object you are
> retrieving.
[...]
> Is this abuse of the object? should I have a separate criteria class?
>
> I'm after the simplest design that I do feel happy about.
Like Susdy, I think this is a valid approach (and one I've used before too).
If your sense of aesthetics, or the nature of the data, make it impossible to
indicate which of an object's attributes are part of the search template and
which are "wildcards", then you could use an auxiliary indication of which
fields to search on and which to ignore. (When I did this, I was working in C
and the attributes had a natural association with integers so it was very
simple just to use a bitmap as the "auxiliary indication", whether there is
anything as handy for you will depend on your application).
-- chris
Fredrik Bertilsson - 24 Aug 2004 10:50 GMT
Have a look at the Query object in http://butler.sourceforge.net.
A Query object uses different subclasses of Filter - like AndFilter,
OrFilter, EqualsFilter - to define the where clause in the select
statement. It can also making joins (Join class) with other tables.
/Fredrik
John C. Bollinger - 24 Aug 2004 14:50 GMT
> I think there is a valid approach to retrieving records based on a criteria
> object that is of the same class as the object you are retrieving.
[quoted text clipped - 19 lines]
>
> Is this abuse of the object? should I have a separate criteria class?
I think it's a bit iffy from an OO purism point of view, but that
doesn't mean it couldn't be made to work fine. If you need to be able
to match against every field and combination of fields then you do need
a criterion object containing at least as much information as a Record,
so there is surely some advantage to using the Record class also for
criteria.
> I'm after the simplest design that I do feel happy about.
If you're unsatisfied with using the same class, then you could consider
using inheritance. The fields and accessor methods could be on a
superclass that does not restrict its field values, and Record could be
a subclass (that need not redefine any accessor methods). You can
stipulate that Record instances may not have null fields (whether or not
you enforce it in code), but that that doesn't necessarily apply to the
superclass. If you like symmetry then you can make the criterion class
be a sibling of Record; otherwise you can just use the superclass.
John Bollinger
jobollin@indiana.edu