Queries that should return single objects

From: Todd O'Bryan (toddobrya..ac.com)
Date: Sun Jul 03 2005 - 22:06:09 EDT

  • Next message: Jürgen Saar: "Re: Expression: NOT EXISTS... JOIN..."

    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) {
         List l = performQuery(q);
         if (l.getSize() != 1) {
             throw new ValidationException("Query expected to return
    single unique object didn't");
         } else {
             return (CayenneDataObject) l.get(0);
    }

    be a reasonable addition, or am I just being silly?

    Thanks,
    Todd



    This archive was generated by hypermail 2.0.0 : Sun Jul 03 2005 - 22:06:13 EDT