App Engine(突然间)告诉我,我的Objectify设置不正确.据工作之前,我做在我的web.xml中的客体筛选.
这是我日志中的完整堆栈跟踪:
javax.servlet.ServletContext log: unavailable
java.lang.IllegalStateException: You have not started an Objectify context.
You are probably missing the ObjectifyFilter.
If you are not running in the context of an http request, see the ObjectifyService.run() method.
at com.googlecode.objectify.ObjectifyService.ofy(ObjectifyService.java:44)
at com.mydomain.gae.defaultmodule.MyObject.loadEverything(MyObject.java:21)
at com.mydomain.gae.defaultmodule.MyServlet.init(MyServlet.java:40)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:206)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:179)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:136)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:469)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308) …Run Code Online (Sandbox Code Playgroud) 我有三个表:items,junction,和properties.我有五个项目(A到E)和五个属性(1到5).通过Junction Table,我已经分配了如下属性:
A: 1, 3, 5
B: 1, 2, 3
C: 1, 4, 5
D: 1, 2, 3
E: 1, 4, 5
Run Code Online (Sandbox Code Playgroud)
当我运行以下查询时,我得到了一个可爱的十五记录笛卡尔积(正如人们所期望的那样).
SELECT I.id, I.item_name, P.property_name FROM scratch.items I
JOIN scratch.junction J ON J.item_id = I.id
JOIN scratch.property P ON J.property_id = P.id;
Run Code Online (Sandbox Code Playgroud)
我想要做的是将每个项目的属性名称连接到一个字段中,这样我就可以像这样吐出它们:
Record | item_id | item_name | properties
----------------------------------------------------------------------------
0 | A | Item A | Property 1, Property 3, Property 5
1 | B | Item B | Property …Run Code Online (Sandbox Code Playgroud) 以下代码有效且可读,但在我看来,我认为不需要中间操作。我编写了这个简化版本,因为实际代码是一个更大过程的一部分。
我有一个Collectionof Widget,每个都有一个名称和多种类型(由WidgetType枚举的常量表示)。这些多种类型都是可以获取的,Stream<WidgetType>但如果有必要,我可以将它们作为其他类型返回。(出于各种原因,强烈希望将它们作为 a 返回,Stream<WidgetType>因为稍后在实际代码中如何使用这些小部件。)
这些小部件被添加到 ,EnumMap<WidgetType, List<Widget>>然后被翻译成EnumMap<WidgetType, Widget[]>.
如果每个都Widget只有一个WidgetType,这将是一个微不足道的解决方案,但是,由于任何 Widget 都可能有 1 个或多个类型,因此我会被Collectors.groupingBy()方法的语法(及其重载)绊倒。
这是代码示例,再次,功能齐全,并为我提供了我需要的确切结果。
class StackOverFlowExample {
private final Map<WidgetType, Widget[]> widgetMap = new EnumMap<>(WidgetType.class);
public static void main(String[] args) { new StackOverFlowExample(); }
StackOverFlowExample() {
Collection<Widget> widgetList = getWidgetsFromWhereverWidgetsComeFrom();
{
final Map<WidgetType, List<Widget>> intermediateMap = new EnumMap<>(WidgetType.class);
widgetList.forEach(w ->
w.getWidgetTypes().forEach(wt -> {
intermediateMap.putIfAbsent(wt, new ArrayList<>());
intermediateMap.get(wt).add(w);
}) …Run Code Online (Sandbox Code Playgroud)