RE: Mapping SQL query results to a Entity Object

From: Gentry, Michael \(Contractor\) ("Gentry,)
Date: Wed Mar 02 2005 - 17:55:54 EST

  • Next message: Gentry, Michael \(Contractor\): "RE: appendOptimisticLockingAttributes map snapshot errorduringinsert/update optimistic locking delete [Was: Re: Does this lookfamiliar?]"

    I'm probably about to give some of my patented bad advice, but ...

    Does a map count as an object you can use? If so, I use something
    similar to get counts:

    // Adjust SELECT as needed
    final String queryString = "SELECT count(t0.createDate) 'rowCount' FROM
    ....";
    final SQLTemplate queryTemplate = new SQLTemplate(AnEntity.class,
    queryString, true);
    queryTemplate.setFetchingDataRows(true);
    List results = dataContext.performQuery(queryTemplate);

    A note about the "AnEntity.class" portion. Even though you won't be
    pulling back a List of AnEntity objects (you get a List of Maps), you
    have to specify that so Cayenne knows which DataNode to use (which makes
    more sense if you have multiple DataNodes). So just plug in an entity
    that makes sense for you there.

    Once you have your results, just iterate over them and ask the map for
    the values by name (use "rowCount" -- or "counts" or whatever you call
    it -- for your count() portion).

    True, it's not an ObjEntity, but still useful for getting reporting
    data. Keep in mind you can't update the values and automatically push
    it back to the database with a commitChanges().

    /dev/mrg

    -----Original Message-----
    From: John Martyniak [mailto:jmarty..x.netcom.com]
    Sent: Wednesday, March 02, 2005 5:39 PM
    To: Cayenne User
    Subject: Mapping SQL query results to a Entity Object

    I would like to find out if it is possible to map the results of a query
    to
    an entity object that doesn't map to a db table.

    For example:

    I have a query something like:
    Select username, count(*) counts from users group by username order by
    counts desc limit 5;

    I would like this to show up in a object that I can then use.

    Is this even possible?

    Or does a obj entity have to map to a db entity? If you go through
    modeler
    it certainly seems like it needs to.

    Any thoughts or suggestions would be very helpful.

    -john



    This archive was generated by hypermail 2.0.0 : Wed Mar 02 2005 - 17:55:59 EST