Re: Queries that should return single objects

From: Mike Kienenberger (mkienen..laska.net)
Date: Tue Jul 05 2005 - 14:59:27 EDT

  • Next message: Daniel Lorimer: "Parenthesis in queries."

    Todd O'Bryan <toddobrya..ac.com> wrote:
    > I find myself writing the same four or five lines of code to perform
    > a query, check that it only returned one object, and then grab that
    > object out.
    >
    > Is this common for other people as well, or is it just me.
    >
    > If it is a pretty common occurrence, can I suggest dropping a
    > convenience method in DataContext?
    >
    > Would
    > public CayenneDataObject performSingletonQuery(SelectQuery q) {
    > be a reasonable addition, or am I just being silly?

    I stick such a method in my BaseDataObject superclass (between
    CayenneDataObject and generated superclasses).

                    if (1 == list.size()) return (BaseDataObject)list.get(0);
                    if (0 == list.size()) return null;
                    throw new MoreThanOneException("Found " + String.valueOf(list.size()) + "
    results");

    -Mike



    This archive was generated by hypermail 2.0.0 : Tue Jul 05 2005 - 14:56:31 EDT