RE: Is it possible to compare entity keys in an object query?

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Dec 10 2004 - 09:51:05 EST

  • Next message: Mike Kienenberger: "Re: Is it possible to compare entity keys in an object query?"

    I guess what Travis wanted is not to match on a known name, but rather
    retrun all pets that match the owner name, regardless of what the name
    is... I just staged a little test using "Master" and "Detail" entities.
    For the exact match this works just fine:

    Expression exp = Expression.fromString("master.name = name");
    SelectQuery q = new SelectQuery(Detail.class, exp);
    context.performQuery(q);

    But for case-insensitive comparison expression BNF only allows a string
    literal on the right-hand side, so code lke this doesn't work:

    exp = Expression.fromString("master.name likeIgnoreCase name")

    I was able to work around it by building the expression manually with this
    ugly code:

    exp = ExpressionFactory.likeIgnoreCaseExp("master.name",
            ExpressionFactory.unaryExp(Expression.OBJ_PATH, "name"));

    But this also means that you can't store such expression in the Modeler
    :-/ Travis, could you open a bug report, we may need to expand the BNF to
    support that
    (http://objectstyle.org/cayenne/grammar/ExpressionParser.html)

    Thanks
    Andrus

    > Hi Travis,
    >
    > Try something like this:
    >
    > final Expression expression = Expression.fromString("name = $name and
    > owner.name = $name");
    > Map parameters = new HashMap(1);
    > parameters.put("name", "Fred");
    > SelectQuery query = new SelectQuery(Pet.class,
    > expression.expWithParameters(parameters));
    > List pets = dataContext.performQuery(query);
    >
    > I seem to recall that if you want to use 'like', that it doesn't handle
    > the $name substitutions very well (I'm sure Andrus will yell at me if I
    > am wrong). If you need to use 'like', try this:
    >
    > String name = "Fred";
    > Expression expression = Expression.fromString("name likeIgnoreCase '%" +
    > name + "%' and owner.name likeIgnoreCase '%" + name + "%'");
    > SelectQuery query = new SelectQuery(Pet.class, expression);
    > List pets = dataContext.performQuery(query);
    >
    >
    > -----Original Message-----
    > From: Travis Cripps [mailto:travi..pparentmotion.com]
    > Sent: Thursday, December 09, 2004 7:31 PM
    > To: cayenne-use..bjectstyle.org
    > Subject: Is it possible to compare entity keys in an object query?
    >
    >
    > Hi. I've been working with Cayenne for about a week. So far, I've
    > found it to be pretty nice.
    >
    > After looking through the documentation and trying to experiment in
    > CayenneModeler, I've been unable to create a query that will compare a
    > key from one entity with a value from a related entity by traversing
    > the relationship.
    >
    > Here's a simple example:
    >
    > Imagine my simple model is:
    >
    > Person (Entity)
    > pk id
    > name
    >
    > Pet (Entity)
    > pk id
    > fk owner_id
    > name
    > owner (to-one relationship with person)
    >
    >
    > What I'd like to do is build a query like:
    >
    > Query Name: PetHasSameNameAsOwnerQuery
    > Query Root: Pet
    > Qualifier: name caseInsensitiveLike owner.name
    >
    > Is this possible in Cayenne? How would I represent that either in
    > CayenneModeler or in an Expression? I'd really appreciate the help.
    >
    > Thank you very much!
    >
    > Travis



    This archive was generated by hypermail 2.0.0 : Fri Dec 10 2004 - 09:51:06 EST