views:

45

answers:

1

Hi,

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)
A: 

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);
Jcs
Thx, I mixed the parameters instructions up. It works now, but I am not sure if its a standard compliant feature to filter interfaces by their properties and if it works with other JDO implementation than DataNucleus. Here is a link to a JDO book: http://www.orientechnologies.com/docs/JavaDataObjects-RobinRoos-1.0.pdf At page 113 there is some explaination about extents and interfaces. In this solution I did not use extents explicitly. The book is from 2003 and may be not up to date at this point. If I get any more information I will post it here.
PageFault