【问题标题】:How to query for an interface and filter the resultset with JDOQL?如何使用 JDOQL 查询接口并过滤结果集?
【发布时间】:2010-09-10 22:02:48
【问题描述】:

我有一个界面

@PersistenceCapable
public interface MyInterface {

    public abstract String getName();

    public abstract void setName(String name);

}

持久层使用 JDO。 JDO 实现是DataNucleus。现在我想用 JDOQL 查询该接口的任何实例。

查询查询 = getPersistenceManager().newQuery(MyInterface.class);

效果很好。现在我想按名称过滤查询结果。当然接口没有属性。其实现的属性可能不同。 MyInterface 的任何实例都通过 getName() 方法返回其名称。

如何在 JDOQL 中使用此方法?

我试了一下:

  1. query.setFilter("name == \"" + name);
  2. query.setFilter("name() == \"" + name);
  3. query.declareVariables("myPackage.MyInterface myInterface"); query.setFilter("myInterface.name == \"" + name);
  4. query.declareVariables("myPackage.MyInterface myInterface"); query.setFilter("myInterface.name() == \"" + name);

JDO 仍然不断抛出异常(请看底部)。我不确定这是否适用于接口,但有一个 get 和一个 set 访问器方法,这是 java beans 术语中的一个属性。我在接口的getName()方法及其实现中添加了@Persistent注解,但没有任何改变。

如何过滤接口实例的查询?

提前致谢。

这是 JDO 抛出的异常:

org.datanucleus.jdo.NucleusJDOHelper getJDOExceptionForNucleusException
INFO: Exception thrown
Invalid string literal (End of stream): identity.name == "Identity 0
org.datanucleus.exceptions.NucleusUserException: Invalid string literal (End of stream): identity.name == "Identity 0
    at org.datanucleus.query.compiler.Lexer.parseStringLiteral(Lexer.java:689)
    at org.datanucleus.query.compiler.JDOQLParser.processLiteral(JDOQLParser.java:1012)
    at org.datanucleus.query.compiler.JDOQLParser.processPrimary(JDOQLParser.java:678)
    at org.datanucleus.query.compiler.JDOQLParser.processUnaryExpression(JDOQLParser.java:621)
    at org.datanucleus.query.compiler.JDOQLParser.processMultiplicativeExpression(JDOQLParser.java:547)
    at org.datanucleus.query.compiler.JDOQLParser.processAdditiveExpression(JDOQLParser.java:518)
    at org.datanucleus.query.compiler.JDOQLParser.processRelationalExpression(JDOQLParser.java:450)
    at org.datanucleus.query.compiler.JDOQLParser.processAndExpression(JDOQLParser.java:427)
    at org.datanucleus.query.compiler.JDOQLParser.processExclusiveOrExpression(JDOQLParser.java:413)
    at org.datanucleus.query.compiler.JDOQLParser.processInclusiveOrExpression(JDOQLParser.java:399)
    at org.datanucleus.query.compiler.JDOQLParser.processConditionalAndExpression(JDOQLParser.java:385)
    at org.datanucleus.query.compiler.JDOQLParser.processConditionalOrExpression(JDOQLParser.java:366)
    at org.datanucleus.query.compiler.JDOQLParser.processExpression(JDOQLParser.java:355)
    at org.datanucleus.query.compiler.JDOQLParser.parse(JDOQLParser.java:93)
    at org.datanucleus.query.compiler.JavaQueryCompiler.compileFilter(JavaQueryCompiler.java:462)
    at org.datanucleus.query.compiler.JDOQLCompiler.compile(JDOQLCompiler.java:101)
    at org.datanucleus.store.query.AbstractJDOQLQuery.compileInternal(AbstractJDOQLQuery.java:302)
    at org.datanucleus.store.query.Query.compile(Query.java:1446)
    at org.datanucleus.jdo.JDOQuery.compile(JDOQuery.java:95)
    at net.x0ver.utilities.AbstractJdoRepository.executeQuery(AbstractJdoRepository.java:40)
    at net.x0ver.identitymanagement.persistence.JdoIdentityRepository.getIdentities(MyJdoRepository.java:29)
    at net.x0ver.identitymanagement.test.MainTest.main(MainTest.java:26)
Exception in thread "main" javax.jdo.JDOUserException: Invalid string literal (End of stream): identity.name == "Identity 0
    at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:396)
    at org.datanucleus.jdo.JDOQuery.compile(JDOQuery.java:99)
    at net.x0ver.utilities.AbstractJdoRepository.executeQuery(AbstractJdoRepository.java:40)
    at net.x0ver.identitymanagement.persistence.JdoIdentityRepository.getIdentities(MyJdoRepository.java:29)
    at net.x0ver.identitymanagement.test.MainTest.main(MainTest.java:26)
NestedThrowablesStackTrace:
Invalid string literal (End of stream): identity.name == "Identity 0
org.datanucleus.exceptions.NucleusUserException: Invalid string literal (End of stream): identity.name == "Identity 0
    at org.datanucleus.query.compiler.Lexer.parseStringLiteral(Lexer.java:689)
    at org.datanucleus.query.compiler.JDOQLParser.processLiteral(JDOQLParser.java:1012)
    at org.datanucleus.query.compiler.JDOQLParser.processPrimary(JDOQLParser.java:678)
    at org.datanucleus.query.compiler.JDOQLParser.processUnaryExpression(JDOQLParser.java:621)
    at org.datanucleus.query.compiler.JDOQLParser.processMultiplicativeExpression(JDOQLParser.java:547)
    at org.datanucleus.query.compiler.JDOQLParser.processAdditiveExpression(JDOQLParser.java:518)
    at org.datanucleus.query.compiler.JDOQLParser.processRelationalExpression(JDOQLParser.java:450)
    at org.datanucleus.query.compiler.JDOQLParser.processAndExpression(JDOQLParser.java:427)
    at org.datanucleus.query.compiler.JDOQLParser.processExclusiveOrExpression(JDOQLParser.java:413)
    at org.datanucleus.query.compiler.JDOQLParser.processInclusiveOrExpression(JDOQLParser.java:399)
    at org.datanucleus.query.compiler.JDOQLParser.processConditionalAndExpression(JDOQLParser.java:385)
    at org.datanucleus.query.compiler.JDOQLParser.processConditionalOrExpression(JDOQLParser.java:366)
    at org.datanucleus.query.compiler.JDOQLParser.processExpression(JDOQLParser.java:355)
    at org.datanucleus.query.compiler.JDOQLParser.parse(JDOQLParser.java:93)
    at org.datanucleus.query.compiler.JavaQueryCompiler.compileFilter(JavaQueryCompiler.java:462)
    at org.datanucleus.query.compiler.JDOQLCompiler.compile(JDOQLCompiler.java:101)
    at org.datanucleus.store.query.AbstractJDOQLQuery.compileInternal(AbstractJDOQLQuery.java:302)
    at org.datanucleus.store.query.Query.compile(Query.java:1446)
    at org.datanucleus.jdo.JDOQuery.compile(JDOQuery.java:95)
    at net.x0ver.utilities.AbstractJdoRepository.executeQuery(AbstractJdoRepository.java:40)
    at net.x0ver.identitymanagement.persistence.JdoIdentityRepository.getIdentities(MyJdoRepository.java:29)
    at net.x0ver.identitymanagement.test.MainTest.main(MainTest.java:26)

【问题讨论】:

    标签: java jdo datanucleus jdoql


    【解决方案1】:

    您的查询似乎格式不正确。异常表示 Invalid string litteral,并且在您的查询示例中,有一个未闭合的双引号字符。也许你应该尝试这样的事情:

    query.setFilter("name == name_value");
    query.declareParameters("String name_value");
    query.execute(name);
    

    【讨论】:

    • 谢谢,我把参数说明混在一起了。它现在可以工作,但我不确定它是否符合标准的功能来按接口的属性过滤接口,以及它是否适用于 DataNucleus 以外的其他 JDO 实现。这是 JDO 书籍的链接:orientechnologies.com/docs/JavaDataObjects-RobinRoos-1.0.pdf 在第 113 页有一些关于范围和接口的解释。在这个解决方案中,我没有明确使用范围。这本书是 2003 年出版的,目前可能不是最新的。如果我得到更多信息,我会在这里发布。
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2015-11-28
    • 2022-10-14
    相关资源
    最近更新 更多