开发者

Why does JDOQL query using "matches" semantics only work with a literal?

开发者 https://www.devze.com 2023-01-17 16:40 出处:网络
I\'m trying to construct a JDOQL query (using datanucleus) that will search for m开发者_Python百科atches of a parent class based on criteria in an owned one-to-many child class.The query looks like th

I'm trying to construct a JDOQL query (using datanucleus) that will search for m开发者_Python百科atches of a parent class based on criteria in an owned one-to-many child class. The query looks like this:

    Query lQ = lPm.newQuery("select from "+Module.class.getName()+
            " where moduleMappings.contains(m)" +
            " && showNameParam.matches(m.criteria.trim())");
    lQ.declareVariables(ModuleMapping.class.getName()+" m");
    lQ.declareParameters("String showNameParam");

lRet = (List<Module>) lQ.execute("StarTrek");

My data set looks something like this:

  • Module[1]
    • ModuleMapping[1]: criteria=".*"
  • Module[2]
    • ModuleMapping[1]: criteria=".*StarTrek.*"
    • ModuleMapping[2]: criteria=".*StarWars.*"

The query never matches on anything! However, if I replace the argument to the matches JDOQL method with a literal:

Query lQ = lPm.newQuery("select from "+Module.class.getName()+
            " where moduleMappings.contains(m)" +
            " && showNameParam.matches('.*StarTrek.*')");

Things will work for that single example, and my query will find Module[2]. What am I missing? Am I not allowed to use the contents of a mapped field as the argument to a JDOQL method? Do I need to escape things in some way?

Dave


So I figured this out, although to me it seems like a bug in either JDOQL or datanucleus. When using a mapped field as the argument to the matches method, the generated SQL does not translate the JDOQL syntax to the syntax of the data store (in my case, SQL). So in my example above, if I change the criteria fields to use SQL wildcard syntax rather than JDOQL syntax things will start to work.

Specifically, if in my example above I use criteria="%StarTrek%" rather than criteria=".\*StarTrek.\*" the JDOQL queries will start to match.

This doesn't seem right to me as different data stores could use different matching syntax, but this workaround gets me moving again...


The log would obviously tell you what SQL is being invoked for your query. You could also address why you are mixing API JDOQL and single-string JDOQL ... definition of parameters/variables should be in the single-string if using single-string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号