Re: Query certain attributes

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Oct 21 2004 - 19:43:46 EDT

  • Next message: Andrus Adamchik: "Re: error at runtime with procedures"

    > If you are actually wanting to limit the number of columns coming back,
    > look at SQLTemplate in section 7.6 of the manual. You won't get
    > DataObjects coming back, though, just raw data rows.

    I assume that Beth was asking exactly that (how to limit the number of
    columns included in result). So let me expand on this some more.
    SQLTemplate is definitely an option like Michael said, but before you
    go this way, make sure this is really what you need.

    So the main question is - do you want to get back a list of Java
    objects mapped via Cayenne (DataObjects), or are you doing ad hoc query
    that doesn't have an object mapping? If you are fetching ad hoc data,
    go with SQLTemplate or (this is a bit of a hack) with SelectQuery with
    columns configured using "SelectQuery .addCustomDbAttribute(..)".

    However if you are fetching an object (say an Artist), all columns that
    map to the object attributes (plus PK and FKs) are fetched. Otherwise a
    valid object can't be created. So if you need an object, but don't need
    all attributes, here is another option - only map a subset of columns
    as ObjAttributes. In the Artist example, you can for example delete
    "dateOfBirth" attribute from the entity, so "DATE_OF_BIRTH" column will
    never be fetched.

    Andrus

    On Oct 21, 2004, at 5:46 PM, Gentry, Michael wrote:
    > Of course. Probably the easiest way is to use one of the Expression
    > methods:
    >
    > Expression expression = Expression.fromString("paintingTitle =
    > $paintingTitle");
    > Map parameters = new HashMap();
    >
    > parameters.put("paintingTitle", "Daisy Verve");
    >
    > SelectQuery query = new SelectQuery(Painting.class,
    > expression.expWithParameters(parameters));
    > List paintings = dataContext.performQuery(query);
    >
    >
    >
    > If you are actually wanting to limit the number of columns coming back,
    > look at SQLTemplate in section 7.6 of the manual. You won't get
    > DataObjects coming back, though, just raw data rows.
    >
    > /dev/mrg
    >
    > -----Original Message-----
    > From: Williams, Beth [mailto:beth.william..FIB.ORG]
    > Sent: Thursday, October 21, 2004 5:32 PM
    > To: cayenne-use..bjectstyle.org
    > Subject: Query certain attributes
    >
    >
    > I have just started working with Cayenne and have a pretty basic
    > question related to querying. Is there a way to only query certain
    > attributes from a db table. From the examples I've seen so far
    > (SelectQuery query = new SelectQuery(Artist.class);) all attributes are
    > returned; equivalent to a SELECT * FROM xxx_table;. Is it possible
    > (and
    > if so, how) to query only certain attributes; equivalent to SELECT
    > painting_title, estimated_price FROM painting;.
    >
    > Hope that makes sense. Thanks.
    >
    > Beth
    >



    This archive was generated by hypermail 2.0.0 : Thu Oct 21 2004 - 19:43:52 EDT