【发布时间】:2014-10-08 20:44:13
【问题描述】:
我真的很想让 Objectify 工作,但我就是想不通为什么它在我的项目中不起作用,所以我改用 JDO 并遇到了这些问题。我正在做一个项目管理应用程序,因此由于一个人可以从事多个项目,并且项目可以有很多人参与,因此我在 Person.java 和 Project.java 之间建立了无主关系
Person.java 包 com.pontuse.appendpoint;
import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Person {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
Long id;
@Persistent
String userName;
@Persistent
String pass;
//Modelling relationship with projects
@Persistent
List<Long> projectsInvolved;
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public List<Long> getProjectsInvolved() {
return projectsInvolved;
}
public void setProjectsInvolved(List<Long> projectsInvolved) {
this.projectsInvolved = projectsInvolved;
}
}
Project.java
package com.pontuse.appendpoint;
import java.util.Date;
import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Project {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
Long id;
@Persistent
Long adminId;
@Persistent
Date deadline;
//Modelling relationship with people
@Persistent
List<Long> peopleOn;
@Persistent
List<Long> tasksIn;
@Persistent
List<Long> tasksDoing;
@Persistent
List<Long> tasksDone;
public Project() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getAdminId() {
return adminId;
}
public void setAdminId(Long adminId) {
this.adminId = adminId;
}
public Date getDeadline() {
return deadline;
}
public void setDeadline(Date deadline) {
this.deadline = deadline;
}
public List<Long> getTasksIn() {
return tasksIn;
}
public void setTasksIn(List<Long> tasksIn) {
this.tasksIn = tasksIn;
}
public List<Long> getTasksDoing() {
return tasksDoing;
}
public void setTasksDoing(List<Long> tasksDoing) {
this.tasksDoing = tasksDoing;
}
public List<Long> getTasksDone() {
return tasksDone;
}
public void setTasksDone(List<Long> tasksDone) {
this.tasksDone = tasksDone;
}
public void setPeopleOn(List<Long> peopleOn) {
this.peopleOn = peopleOn;
}
public List<Long> getPeopleOn() {
return peopleOn;
}
}
所以,我尝试执行一个查询,该查询将返回一个人被分配到的所有项目。这是我目前所拥有的:
PojectEndpoint.java
@ApiMethod(name = "getPersonsProjects")
public CollectionResponse<Project> getPersonsProjects(@Named("projects") List<Long> projects){
PersistenceManager mgr = null;
List<Project> execute = new ArrayList<Project>();
for(Long l: projects)
execute.add(mgr.getObjectById(Project.class, l));
mgr.close();
return CollectionResponse.<Project> builder().setItems(execute).build();
}
到目前为止,当我提供有效项目 ID 列表时,我只有 404
404 Not Found
- Hide headers -
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Length: 29
Content-Type: text/html; charset=UTF-8
Date: Wed, 08 Oct 2014 20:38:05 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Pragma: no-cache
Server: GSE
Not Found
如果我只输入一个(仍然有效的)id,我会得到一个 NullPointerException
503 Service Unavailable
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.NullPointerException"
}
],
"code": 503,
"message": "java.lang.NullPointerException"
}
}
有人能看到发生了什么吗?或者我是否完全错误地实现了该方法?
【问题讨论】:
-
您是否可以在端点方法中放置一个try catch,然后记录异常和堆栈跟踪,以查看是哪一行导致了问题。
-
如果你还想在 Objectify 上再试一次,试试这个 Guice/Jersey/Objectify/Endpoint 样板应用程序github.com/omerio/appstart
-
NullPointerException - 可能是因为你设置了
PersistenceManager mgr = null然后调用mgr.getObjectById(..)?? -
好的,我设置了持久性管理器,如果我提供一个包含一个值的列表,我现在可以让它返回一个项目。两个值,它仍然返回 404。
-
声明很明显:你不需要到处都是@Persistent。大多数这些类型默认为持久(使代码难以阅读)。不知道你的“查询”是什么。我看不到 pm.newQuery。如果您的意思是 pm.getObjectById 那么该方法返回什么?
标签: java google-app-engine rest google-cloud-endpoints jdo