开发者

How to query for an interface and filter the resultset with JDOQL?

开发者 https://www.devze.com 2023-01-15 11:58 出处:网络
I have an interface @PersistenceCapable public interface MyInterface { public abstract String getName();

I have an interface

@PersistenceCapable
public interface MyInterface {

    public abstract String getName();

    public abstract void setName(String name);

}

The persistence layer uses JDO. The JDO implementation is DataNucleus. Now I want to query for any instances of that interface with JDOQL.

开发者_运维问答

Query query = getPersistenceManager().newQuery(MyInterface.class);

That works fine. Now I want to filter the query results by name. Of course the interface has no attributes. The attributes of its implementation may differ. Any instance of MyInterface returns its name by the getName() method.

How do I use this method with JDOQL?

I tried a bit:

  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 still keeps throwing exceptions (look at the bottom). I am not shure if this is applicable to interfaces, but there is a get and a set accesor method, which is a property in java beans terms. I added the @Persistent annotation to the getName() method of the interface and its implementation, but nothing changed.

How do I filter a query for interface instances?

Thanks in advance.

Here is the exception JDO throws:

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)


It seems that your query is not well formed. The exception indicates Invalid string litteral and, in your query sample, there is an unclosed double quote character. Maybe you should try something like that:

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

精彩评论

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

关注公众号