Re: Thanks, and back to enums

From: Mike Kienenberger (mkienen..laska.net)
Date: Mon Apr 05 2004 - 12:44:19 EDT

  • Next message: Mike Kienenberger: "Re: Thanks, and back to enums"

    Todd O'Bryan <toddobrya..ac.com> wrote:
    > Is there an easier way to get a unique object than getting a list, and
    > getting the first item in it?

    Not that I know of.

    > Should I create a static getRole(String value) method in the Role
    > object? If so, should I pass in the DataContext, since I'm creating one
    > per Tapestry Visit object (aka HttpSession, kinda) and don't want the
    > overhead of making a bunch?
    >
    > Is there a shortcut for fetching objects based on what should be a
    > unique key (like "admin" in this case, or a user's login)? That would
    > be really nice. Something like

    I put this in the super-class of my cayenne-generated data objects.

    Ie,

            CayenneDataObject
                    GenericEntity
                            _CustomEntity1
                                    CustomEntity1
                            _CustomEntity2
                                    CustomEntity2
                            (etc)

            public static GenericEntity objectMatchingKeyAndValue(DataContext
    aDataContext, Class aClass, String anAttributeName, Object aValue)
            {
                    List aList = objectsMatchingKeyAndValue(aDataContext, aClass,
    anAttributeName, aValue);
                    if (1 == aList.size()) return (GenericEntity)aList.get(0);
                    if (0 == aList.size()) return null;
                    throw new MoreThanOneException("Found " + String.valueOf(aList.size()) + "
    results");
            }

            public static List objectsMatchingKeyAndValue(DataContext aDataContext,
    Class aClass, String anAttributeName, Object aValue)
            {
                    Expression qualifier = ExpressionFactory.matchExp(anAttributeName,
    aValue);
                    SelectQuery query = new SelectQuery(aClass, qualifier);
                    return aDataContext.performQuery(query);
            }
            



    This archive was generated by hypermail 2.0.0 : Mon Apr 05 2004 - 12:43:19 EDT