Re: How to create JOIN query

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Jun 16 2004 - 12:04:07 EDT

  • Next message: Andrus Adamchik: "Re: How to create JOIN query"

    Joe, you are right, object queries with object property path traversal
    are the analogs of DB joins. Donald just sent me his schema directly,
    this is a standard case of a to-one relationship. It is handled well by
    a simple object (no SQL) SelectQuery. So Donald's query would look like
    this:

    // assume two related tables are mapped as Object and Concept classes
    // "Object" is not java.lang.Object, but some other class, say
    my.xyz.Object

    Expression qualifier = Expression.fromString("toConcept.name =
    'Project'");
    SelectQuery query = new SelectQuery(Object.class, qualifier);
    List results = dataContext.performQuery(query);

    > If you have a complex relationship that is messy to do in objects, try
    > creating a view.  I think (but cannot figure out) there is a way to
    > create a dynamic view in Cayenne.

    > I, too, would like something like  Public Tree (or some
    > structure) Context.query(List tables, Expression expression) (probably
    > not OO, but at least one could get all the results in a single query).

    SQLTemplate provides functionality of a dynamic view. But if you can
    use SelectQuery above, don't use SQLTemplate, as this kind of defeats
    the purpose of ORM! It makes sense to use SQLTemplate in those complex
    cases that have no direct object property path mapping. It works
    similar to iBatis framework, but is fully integrated into ORM (for
    instance you can SELECT stuff with SQLTemplate, and
    update/delete/insert via "normal" Cayenne means).

    http://objectstyle.org/cayenne/userguide/fetch/sqltemplate.html
    http://objectstyle.org/cayenne/userguide/fetch/sqltemplate-
    scripting.html

    1.1M7 supports creating SQLTemplates in the Modeler. On the rare
    occasion when you want to run the same query on different DBs, SQL
    customization by DB is encapsulated within the query, so your Java code
    still stays 100% cross-db.

    Andrus

    On Jun 16, 2004, at 11:36 AM, McDaniel, Joe R. wrote:

    > The idea is to use object relationships to do the "join" not a join,
    > per se to get the data.  (I have trouble with this idea, too.) You can
    > add a condition in the query to limit the results on the initial
    > table, but to get the data use the toXXX (assuming you have a foreign
    > key relationship, of course). 
    >  
    > If you have a complex relationship that is messy to do in objects, try
    > creating a view.  I think (but cannot figure out) there is a way to
    > create a dynamic view in Cayenne.
    >  
    > I, too, would like something like  Public Tree (or some
    > structure) Context.query(List tables, Expression expression) (probably
    > not OO, but at least one could get all the results in a single query).
    >  
    > Best,
    >  
    > Joe
    > -----Original Message-----
    > From: Donald Mackinnon [mailto:donald.mackinno..houghtweb.com]
    > Sent: Wednesday, June 16, 2004 9:47 AM
    > To: 'cayenne-use..bjectstyle.org'
    > Subject: How to create JOIN query
    >
    >  
    > Hi
    >  
    > I am new to Cayenene and am not sure about the best way to query 2
    > tables.
    >  
    > Is it best to create a Flattened Relationship using the Cayenne
    > modeler?
    >  
    >  
    > Thanks
    >  
    > Donald
    >
    >  



    This archive was generated by hypermail 2.0.0 : Wed Jun 16 2004 - 12:04:12 EDT