Re: Mapping SQL query results to a Entity Object

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Mar 02 2005 - 19:00:51 EST

  • Next message: Mike Kienenberger: "Re: Help with Cayenne Deployment?"

    On Mar 2, 2005, at 6:29 PM, John Martyniak wrote:

    > Thanks for the info.
    >
    > I kind of thought that is how it would have to be done.
    >
    > But I thought that it would be cool to have a application based view
    > of the
    > data. Kind of a way to get around not having views in mysql. But it
    > would
    > be more useful because you wouldn't have to involve the DBAs to make a
    > change. The other advantage would be to create a manipulated view of
    > data.

    This feature close to the top on my TODO list (just like many other
    things :-)) and mostly for the same reasons that you mention here.
    Currently you have a number of reasonable options.

    1. Use DataRows (maps) - this is what Michael suggested.

    2. Map a read-only ObjEntity to a fake DbEntity that has the columns of
    the view.

    3. As SQLTemplate scripting allows you to name the columns of the
    result using arbitrary labels, you can create a POJO class that
    describes the query (without ObjEntity or DbEntity), and use something
    like commons-beanutils to manually convert between the map and your
    Java class.

    4. Create a superclass of your custom "view" Java classes that stores
    its properties in a map. Then any concrete subclass would implement
    getters and setter accessing the underlying map. (EOGenericRecord and
    CayenneDataObject look-alike).

    The point of (3) and (4) is that you don't even need a backing entity
    mapping, just the right SQLTemplate... And then you can take a list of
    data rows and in one step convert them to non-persistent objects that
    are not even known to Cayenne. I guess such conversion method would be
    a nice addition to DataObjectUtils, but I have to think it through
    first.

    Andrus



    This archive was generated by hypermail 2.0.0 : Wed Mar 02 2005 - 19:00:55 EST