Question

In the Apache Cayenne documentation, they provide an example of how to create a parameterized query using the Expression class' fromString() function:

// create a qualifier with two named parameters: "pname" and "aname"
Expression qual = Expression.fromString("paintingTitle = $pname or toArtist.artistName = $aname");

// build a query prototype of a query - simply another select query
SelectQuery proto = new SelectQuery(Painting.class, qual);

The making of such a query is pretty straightforward, except for one problem: the documentation does not explain what $pname and $aname are or how to set them to the values you want to query for!

Can anyone explain how to set these parameters??? Please advise...

Était-ce utile?

La solution

You are probably checking older documentation. Check out "Named Parameter Expressions" here if you are on Cayenne 3.0, or "Creating Expressions from Strings" here for 3.1. But in any event, this is fairly simple - you put your parameters in a Map, and then use "expWithParameters" method. To follow your example:

Expression qual = 
   Expression.fromString("paintingTitle = $pname or toArtist.artistName = $aname");
Map<String, Object> params = new HashMap<>();
params.put("pname", "A");
params.put("aname", "B");

qual = qual.expWithParameters(params);

Note that in the last line I am reassigning the Expression, as 'expWithParameters' creates a clone.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top