- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这道题的基本思路如下。我执行一些查询
List<Class2> class2entities = ObjectifyService.ofy()
.load()
.type(Class2.class)
.filter(?,?)
.list();
我可以用什么方式执行另一个基于 class2entities 的查询?
实际上,我正在开发基于 Objectify 的 GAE 应用程序。我使用以下实体
问题:
@Entity
public class Problem {
@Id public String problemname;
public Problem (String name) {
problemname = name;
}
public Problem () {
problemname = "nullProblem";
}
}
元组:
@Entity
public class Tuple {
@Parent Ref<Problem> theProblem;
@Index public String tuple_id;
@Id public Long id;
public Tuple()
{
String s="empty operator";
}
public Tuple(String sid, Problem problem)
{
tuple_id = sid;
try {
theProblem = Ref.create(problem);
}
catch (java.lang.NullPointerException e)
{
System.out.println(e.toString() + " tuple in datastore was not created because of Problem is empty" );
}
}
}
属性:
@Entity
public class Attribute {
@Parent com.googlecode.objectify.Ref<Problem> theProblem;
@Id public Long id;
public String attributeName;
@Index public String attributeFieldName;
@Index public Date date;
/**
* Simple constructor just sets the date
**/
public Attribute() {
date = new Date();
}
/**
* A connivence constructor
**/
public Attribute(Problem problem, String attributeName) {
this();
if( problem != null ) {
theProblem = Ref.create(problem); // Creating the Ancestor key
} else {
theProblem = Ref.create(new Problem("nullProblem"));
}
this.attributeName = attributeName;
}
/**
* Takes all important fields
**/
public Attribute(Problem problem, String attributeName, String var_attributeFieldName) {
this(problem, attributeName);
attributeFieldName = var_attributeFieldName;
}
}
分类数据:
@Entity
public class CategorizedData {
@Load public Ref<Attribute> theAttribute;
@Id Long id;
@Parent public Key<Tuple> theTuple;
public String attributeValue;
@Index public Date date;
/**
* getter and setter for theAttribute
**/
public Attribute getAttribute() { return theAttribute.get(); }
public void setAttribute(Attribute attribute) { theAttribute = Ref.create(attribute); }
/**
* Simple constructor just sets the date
**/
public CategorizedData() {
date = new Date();
}
/**
* A connivence constructor
**/
public CategorizedData(String tupleId, String attribute_field_name, String var_attributeValue) {
this();
Attribute attribute = ofy().load().type(Attribute.class).filter("attributeFieldName", attribute_field_name).first().now();
Tuple tuple = ofy().load().type(Tuple.class).filter("tuple_id",tupleId).first().now();
if( tupleId != null ) {
theTuple = Key.create(Tuple.class, tuple.id); // Creating the Ancestor key
} else {
theTuple = Key.create(Tuple.class, (new Tuple()).id);
}
if( attribute != null ) {
theAttribute = Ref.create(attribute); // Creating the Ancestor ref
} else {
theAttribute = Ref.create(new Attribute());
}
this.attributeValue = var_attributeValue;
}
}
现在我想获取给定问题的所有元组实体,并获取具有给定 AttributeFieldName 的属性字段的 CategorizedData 的所有实体。
我需要做类似的事情
Key<Problem> theProblem = Key.create(Problem.class, problemName);
// Run an ancestor query
List<Tuple> tuples = ObjectifyService.ofy()
.load()
.type(Tuple.class)
.ancestor(theProblem)
.list();
然后我需要在此列表元组中获取 CategorizedData 的实体。我应该怎么办?是否可以将先前查询的结果用于 Objectify 查询而不是所有数据存储?请帮助我...
最佳答案
您的问题有几个解决方案。但首先我想说您的实体可能针对数据存储设计不佳。使用数据存储时,您应该通过分析要查询的内容来创建实体。
一个例子:
你有一个 Problem
实体和一个Tuple
实体但你的Problem
实体只有一个属性 ( problemname
)。数据存储方式是非规范化这种关系并将 problemname
属性到你的 Tuple
实体(或 Tuple
属性到 Problem
)。这样你就可以开始查询你的 Tuple
entitiy 并且您已经拥有正式两个实体的数据。是的,这意味着您的数据存储中将有冗余数据,但从数据存储的角度来看这很好。
现在来解决你的实际问题
Objectify 允许您设置 @Load
对 Ref<Something>
的注释. Objectify 将对此做的是
load().keys(<ListFrom2>)
Ref#get()
- 方法及其背后的实体已经加载因此您可以使用 @Load 注释,但即使您不需要它们,它也会始终加载 Refs。我通常自己创建一个加载数据的方法。基本上我实现了 1-4 中的所有步骤,除了没有 @Load 注释并且我的方法创建了键列表来加载自身。这里唯一的问题是将结果 Entities 匹配回 Ref(实际上这不是一个大问题,因为以集合作为参数的键查询的结果是一个以实体键作为键的 Map)
其次,如果你想/必须保持你的实体那样。尝试减少对数据存储的调用量并在内存中进行一些排序:
ofy.load().ancestor(Key.create(Problem.class, "ProblemName"))
会给你Problem
和所有相关的Tuple
s 和 Attribute
秒。如果您需要 CategorizedData
同样,制作CategorizedData
Problem
的 child 并引用 Attribute
用一个简单的 Ref<Attribute>
没有@Parent。再说一次:如果这是我的代码,我会制作 CategroizedData
嵌入式 List
的 Attribute
.在处理祖先查询的结果时,您可以自己进行各种过滤和排序。这比执行数百个查询要快得多。
总而言之,我认为您的问题源于在设计实体时没有考虑数据的实际使用。尽可能去规范化,将冗余数据视为一种选择,而不是不可行的。
关于java - GAE Objectify如何将查询结果用于另一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139350/
在 GAE 中,我有一个应用程序将文件存储在 GAE blobstore 服务中。数据存储后,还应将其发送到未部署在 GAE 上的外部 Web 服务。我使用Java。 我的第一个想法是使用 JAX-R
我想找到一个用于验证用户、存储用户的 webapp 框架 并且具有jquery的ajax效果, 那么,你知道这个简单的框架吗? 谢谢 喜欢这个页面:http: //digu.com/reg 最佳答案
为了重新部署 GAE 应用程序,我目前必须在我用于部署的系统上安装 GAE 部署工具。虽然这个过程相对简单,但部署过程是一个手动过程,不能在防火墙后运行,并且部署工具必须安装在每台将用于更新 GAE
This链接显示 GAE 不支持开发服务器上的 cron 作业。那么我的其他选择是什么? 当用户的帐户截止日期已到时,我需要向他们发送电子邮件。 最佳答案 使用本地 cron 服务,并编写一个使用 c
我在运行 Maven GAE Plugin 的 gae:debug 目标时遇到问题.我收到的错误如下。有什么想法吗? 我用“mvn gae:debug”运行它。 [INFO] Packaging we
Google 刚刚宣布支持 App Engine 的 PHP 运行时。我有一个使用 Java 运行时开发的应用程序,它利用了 native App Engine 数据存储区。它目前用作移动客户端的后端
是否有一些前端允许管理员从 Google 应用引擎数据存储区插入、更新、删除记录以及创建修改和删除表,就像您在 mysql 上使用 phpMyAdmin 所做的那样??? 最佳答案 Google Ap
我正在以 GAE 为优势实现 PPO。以下代码是我根据OpenAI的基线实现计算GAE和返回的方式。 advantages = np.zeros_like(rewards) last_adv = 0
这是一个[python代码][1],我想知道它是否也可以用于GAE Java(当代码迁移时)。所以问题是,下面的 python 代码是否可以转换为 Java,而无需任何 Java 所没有的 pytho
当在 GAE 中收到一个 http 帖子时,我从另一个服务器下载一个 txt 文件,进行一些解析,然后返回信息。但是,当我更新这个 txt 文件并尝试通过 GAE 访问它时,似乎有延迟。这是一步一步的
当我运行 gae:run 时,它成功构建 jar 并启动服务器。但看起来服务器没有完全启动,因为我无法转到请求的页面。 当我使用 IDE(不是 maven gae 插件)和由 maven gae 插件
我知道有这样的插件可以在 vim 中调试 python:https://github.com/joonty/vdebug 我正在努力寻找调试 GAE 应用程序的方法,这可能吗?如果是这样,我应该采取什
我在 GAE 上部署了一个 webapp2 python 应用程序。有什么方法可以让我从 GAE 控制台探索源代码或更改项目文件。如果我只想更新已部署的应用程序上的单个 .py 文件而不是再次部署整个
我有一个 Google App 引擎应用程序,我希望它的工作方式有所不同,具体取决于它是在我的本地开发环境中运行(即使用 dev_appserver.py)还是在实际的 GAE 云中运行。 目前,我使
阅读 GAE NDB 数据存储的新文档: https://cloud.google.com/appengine/docs/python/ndb/modelclass#class_methods get
今天我第一次上传我的应用程序,当第一个请求进入以触发应用程序初始化时,它失败了。我已经在 Google GAE DEV 服务器上本地测试了所有内容,Google DEV 服务器和真正的 GAE 环境之
我正在使用 GAE 构建应用程序,并想为此使用 Django。哪个“补丁”更好? app-engine-patch 还是 django-gae-helpers?我指的是它们的功能和 future (其
使用 Google App Engine 标准 Python 2.7,我的dispatch.yaml 中有一个路径来指定“*/flex/*”类型的所有网址以路由到 Flex 服务。 调度.yaml调度
我需要一份关于使用 Flex+BlazeDS+Spring+GAE 的可读教程(在 GAE 数据存储中存储多个表)。有这样的吗? 最佳答案 你必须一步一步来: http://www.springsou
我们要重复 spring-boot-sample-gae 的指令一步一步,但我们仍然无法创建 WAR 文件,并出现以下错误消息: [错误] 无法在项目 gae-demo 上执行目标:无法解析项目 or
我是一名优秀的程序员,十分优秀!