App Engine 数据存储 - 查询枚举字段

发布于 2024-08-23 14:26:37 字数 560 浏览 4 评论 0原文

我使用 GAE(Java) 和 JDO 来实现持久性。

我有一个带有 Enum 字段的实体,该字段被标记为 @Persistent 并正确保存到数据存储中(如从开发控制台中的数据存储查看器观察到的)。但是,当我查询这些基于枚举值放置过滤器的实体时,它总是返回所有实体,无论我为枚举字段指定什么值。

我知道 GAE java 支持像基本数据类型一样持久保存枚举。但它是否也允许基于它们进行检索/查询?谷歌搜索无法向我指出任何此类示例代码。

细节:

我在执行之前打印了查询。因此,在两种情况下,查询看起来像这样 -

SELECT FROM com.xxx.yyy.User WHERE role == super ORDER BY key desc RANGE 0,50

SELECT FROM com.xxx.yyy.User WHERE role == admin ORDER BY key desc RANGE 0,50

尽管数据存储查看器显示一些用户的类型为“admin”,一些用户的类型为“super”,但上述两个查询都返回数据存储中的所有用户实体。

I am using GAE(Java) with JDO for persistence.

I have an entity with a Enum field which is marked as @Persistent and gets saved correctly into the datastore (As observed from the Datastore viewer in Development Console). But when I query these entities putting a filter based on the Enum value, it is always returning me all the entities whatever value I specify for the enum field.

I know GAE java supports enums being persisted just like basic datatypes. But does it also allow retrieving/querying based on them? Google search could not point me to any such example code.

Details:

I have printed the Query just before being executed. So in two cases the query looks like -

SELECT FROM com.xxx.yyy.User WHERE role == super ORDER BY key desc RANGE 0,50

SELECT FROM com.xxx.yyy.User WHERE role == admin ORDER BY key desc RANGE 0,50

Both above queries return me all the User entities from datastore in spite of datastore viewer showing some Users are of type 'admin' and some are of type 'super'.

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

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

发布评论

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

评论(3

绝不放开 2024-08-30 14:26:37

目前,我用简单的整数常量替换了枚举。将此案例报告为 Google 应用引擎中的问题:http:// code.google.com/p/googleappengine/issues/detail?id=2927

For time being, I have replaced the Enums with simple integer constants. Reported this case as an issue in the google app engine : http://code.google.com/p/googleappengine/issues/detail?id=2927

并安 2024-08-30 14:26:37

对于除 String 或 int 之外的参数,我相信您需要使用 declareParameters 来代替。尝试这样的事情:

Query q = pm.newQuery(com.xxx.yyy.User.class);
q.setFilter("role == p1");  //p1 is a variable place holder
q.declareParameters("Enum p1"); //here you define the data type for the variable, in this case an Enum
q.setRange(0, 50);
q.setOrdering("key desc");

AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin);

或者如果你想要更多类似 gql 的语法 -

Query query = pm.newQuery("SELECT FROM com.xxx.yyy.User WHERE role == p1 ORDER BY key desc RANGE 0,50");
query.declareParameters("Enum p1");
AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin);

For a parameter other than a String or an int, I believe you need to use declareParameters instead. Try something like this:

Query q = pm.newQuery(com.xxx.yyy.User.class);
q.setFilter("role == p1");  //p1 is a variable place holder
q.declareParameters("Enum p1"); //here you define the data type for the variable, in this case an Enum
q.setRange(0, 50);
q.setOrdering("key desc");

AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin);

or if you want more gql like syntax -

Query query = pm.newQuery("SELECT FROM com.xxx.yyy.User WHERE role == p1 ORDER BY key desc RANGE 0,50");
query.declareParameters("Enum p1");
AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin);
掐死时间 2024-08-30 14:26:37

声明查询参数时,您需要使用枚举的类名。

例如,如果您使用方法样式构建查询,并假设您的枚举名为 Role 并在 User 类下声明,则可以执行如下操作:

Query query = pm.newQuery(com.xxx.yyy.User.class);
query.setFilter("role == roleParam");
query.declareParameters(com.xxx.yyy.User.Role.class.getName() + " roleParam");

You need to use your enum's class name when you declare the query parameter.

For example, if you build your query using the method style, and assuming your enum is called Role and is declared under the User class, you can do something like the following:

Query query = pm.newQuery(com.xxx.yyy.User.class);
query.setFilter("role == roleParam");
query.declareParameters(com.xxx.yyy.User.Role.class.getName() + " roleParam");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文