在 Google App Engine 中使用 JDO 查询集合属性

发布于 2024-10-02 12:31:05 字数 8926 浏览 2 评论 0原文

我有两个类 PaymentJDO 和 PaymentItemJDO。一次付款可以有多个项目。任何付款项目都与发票相关联。 我想检索与特定发票相关的每笔付款。我有发票ID可以查询。

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class PaymentJDO implements BaseOwnedObject, BasicBean {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    protected Long id;

    @Persistent
    private Date date;

    @Persistent
    private String customerId;

    @Persistent
    private String pelicamoId = null;

    @Persistent
    @Element(dependent = "true")
    private List<PaymentItemJDO> items;

    public void setId(Long id) { this.id = id;}

    public String getId() {
        if( id == null ) return null;
        return id.toString();
    }
    public void setId(String id) {
        if(id!=null) {
            this.id = Long.parseLong(id);
        }   
    }

    public Date getDate() { return date;}
    public void setDate(Date date) { this.date = date;}

    public String getCustomerId() { return customerId;}
    public void setCustomerId(String customerId) { this.customerId = customerId;}

    public List<PaymentItemJDO> getItems() { return items;}
    public void setItems(List<PaymentItemJDO> items) {this.items = items;}


    public String getPelicamoId() { return pelicamoId;}
    public void setPelicamoId(String pelicamoId) { this.pelicamoId = pelicamoId;}
}


@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class PaymentItemJDO {   
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    protected Key id;

    @Persistent
    private String invoiceId;

    @Persistent
    private double amount;

    public PaymentItemJDO(String invoiceId) {
        setInvoiceId(invoiceId);
    }

    //override equals method for List .contains
    @Override
    public boolean equals(Object item) {
        return ((PaymentItemJDO)item).getInvoiceId().equals(this.invoiceId);
    } 

    public Key getId() { return id;}

    public String getInvoiceId() { return invoiceId;}
    public void setInvoiceId(String invoiceId) { this.invoiceId = invoiceId;}

    public double getAmount() { return amount;}
    public void setAmount(double amount) { this.amount = amount;}
}

我有下一个函数来查询与特定发票相关的付款

public List<PaymentJDO> getJDOPayments(String invoiceId)  throws SessionExpiredException {
    PersistenceManager pm = JdoUtil.getPm();        
    Query query = pm.newQuery(PaymentJDO.class);
    String pelicamoId = getCurrentPelicamoId();
    query.setFilter("items.contains(:invoice)");
    return (List<PaymentJDO>) query.execute(new PaymentItemJDO(invoiceId));
}

我收到下一个错误

javax.jdo.JDOFatalUserException: Illegal argument
    at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:344)
    at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:252)
    at com.softamo.pelicamo.server.MainServiceImpl.getJDOPayments(MainServiceImpl.java:947)
    at com.softamo.pelicamo.server.MainServiceImpl.getIncomeSummaries(MainServiceImpl.java:817)
    at com.softamo.pelicamo.server.IncomeServiceTest.testGetIncomeSummaries(IncomeServiceTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
NestedThrowablesStackTrace:
java.lang.IllegalArgumentException: items: com.softamo.pelicamo.server.jdo.PaymentItemJDO is not a supported property type.
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:184)
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:157)
    at com.google.appengine.api.datastore.Query$FilterPredicate.<init>(Query.java:549)
    at com.google.appengine.api.datastore.Query.addFilter(Query.java:235)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addLeftPrimaryExpression(DatastoreQuery.java:1138)
    at org.datanucleus.store.appengine.query.DatastoreQuery.handleContainsOperation(DatastoreQuery.java:987)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addExpression(DatastoreQuery.java:880)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addFilters(DatastoreQuery.java:827)
    at org.datanucleus.store.appengine.query.DatastoreQuery.performExecute(DatastoreQuery.java:228)
    at org.datanucleus.store.appengine.query.JDOQLQuery.performExecute(JDOQLQuery.java:89)
    at org.datanucleus.store.query.Query.executeQuery(Query.java:1489)
    at org.datanucleus.store.query.Query.executeWithArray(Query.java:1371)
    at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:243)
    at com.softamo.pelicamo.server.MainServiceImpl.getJDOPayments(MainServiceImpl.java:947)
    at com.softamo.pelicamo.server.MainServiceImpl.getIncomeSummaries(MainServiceImpl.java:817)
    at com.softamo.pelicamo.server.IncomeServiceTest.testGetIncomeSummaries(IncomeServiceTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I have two classes PaymentJDO and PaymentItemJDO. A payment can have several items. Any payment item is associated with an invoice.
I want to retrieve every payment which is related to a particular invoice. I have the invoice Id to query.

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class PaymentJDO implements BaseOwnedObject, BasicBean {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    protected Long id;

    @Persistent
    private Date date;

    @Persistent
    private String customerId;

    @Persistent
    private String pelicamoId = null;

    @Persistent
    @Element(dependent = "true")
    private List<PaymentItemJDO> items;

    public void setId(Long id) { this.id = id;}

    public String getId() {
        if( id == null ) return null;
        return id.toString();
    }
    public void setId(String id) {
        if(id!=null) {
            this.id = Long.parseLong(id);
        }   
    }

    public Date getDate() { return date;}
    public void setDate(Date date) { this.date = date;}

    public String getCustomerId() { return customerId;}
    public void setCustomerId(String customerId) { this.customerId = customerId;}

    public List<PaymentItemJDO> getItems() { return items;}
    public void setItems(List<PaymentItemJDO> items) {this.items = items;}


    public String getPelicamoId() { return pelicamoId;}
    public void setPelicamoId(String pelicamoId) { this.pelicamoId = pelicamoId;}
}


@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class PaymentItemJDO {   
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    protected Key id;

    @Persistent
    private String invoiceId;

    @Persistent
    private double amount;

    public PaymentItemJDO(String invoiceId) {
        setInvoiceId(invoiceId);
    }

    //override equals method for List .contains
    @Override
    public boolean equals(Object item) {
        return ((PaymentItemJDO)item).getInvoiceId().equals(this.invoiceId);
    } 

    public Key getId() { return id;}

    public String getInvoiceId() { return invoiceId;}
    public void setInvoiceId(String invoiceId) { this.invoiceId = invoiceId;}

    public double getAmount() { return amount;}
    public void setAmount(double amount) { this.amount = amount;}
}

I have the next function to query the payments related to a particular invoice

public List<PaymentJDO> getJDOPayments(String invoiceId)  throws SessionExpiredException {
    PersistenceManager pm = JdoUtil.getPm();        
    Query query = pm.newQuery(PaymentJDO.class);
    String pelicamoId = getCurrentPelicamoId();
    query.setFilter("items.contains(:invoice)");
    return (List<PaymentJDO>) query.execute(new PaymentItemJDO(invoiceId));
}

I get the next error

javax.jdo.JDOFatalUserException: Illegal argument
    at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:344)
    at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:252)
    at com.softamo.pelicamo.server.MainServiceImpl.getJDOPayments(MainServiceImpl.java:947)
    at com.softamo.pelicamo.server.MainServiceImpl.getIncomeSummaries(MainServiceImpl.java:817)
    at com.softamo.pelicamo.server.IncomeServiceTest.testGetIncomeSummaries(IncomeServiceTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
NestedThrowablesStackTrace:
java.lang.IllegalArgumentException: items: com.softamo.pelicamo.server.jdo.PaymentItemJDO is not a supported property type.
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:184)
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:157)
    at com.google.appengine.api.datastore.Query$FilterPredicate.<init>(Query.java:549)
    at com.google.appengine.api.datastore.Query.addFilter(Query.java:235)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addLeftPrimaryExpression(DatastoreQuery.java:1138)
    at org.datanucleus.store.appengine.query.DatastoreQuery.handleContainsOperation(DatastoreQuery.java:987)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addExpression(DatastoreQuery.java:880)
    at org.datanucleus.store.appengine.query.DatastoreQuery.addFilters(DatastoreQuery.java:827)
    at org.datanucleus.store.appengine.query.DatastoreQuery.performExecute(DatastoreQuery.java:228)
    at org.datanucleus.store.appengine.query.JDOQLQuery.performExecute(JDOQLQuery.java:89)
    at org.datanucleus.store.query.Query.executeQuery(Query.java:1489)
    at org.datanucleus.store.query.Query.executeWithArray(Query.java:1371)
    at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:243)
    at com.softamo.pelicamo.server.MainServiceImpl.getJDOPayments(MainServiceImpl.java:947)
    at com.softamo.pelicamo.server.MainServiceImpl.getIncomeSummaries(MainServiceImpl.java:817)
    at com.softamo.pelicamo.server.IncomeServiceTest.testGetIncomeSummaries(IncomeServiceTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

已下线请稍等 2024-10-09 12:31:05

你不需要声明你的参数吗?

query.declareParameters("PaymentItemJDO invoice");

Don't you need to declare your parameter?

query.declareParameters("PaymentItemJDO invoice");
爱,才寂寞 2024-10-09 12:31:05

显然GAE/J的插件不支持“collField.contains(parameter)”。为什么他们不这样做,只有他们自己才能回答。

Obviously GAE/J's plugin doesn't support "collField.contains(parameter)". Why they don't only they can answer.

尐籹人 2024-10-09 12:31:05

AFAIK“包含”的作用正好相反。当作为集合的查询参数包含实体的属性值时,它就满足。

在你的情况下,“items ==发票参数”应该可以解决问题

AFAIK "contains" works the other way around. It is satisfied, when a query parameter that is a collection contains a property value of an entity.

In your case, "items == invoiceParameter" should do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文