获取具有连接表GAE的实体

时间:2015-06-08 20:36:28

标签: google-app-engine jpa google-cloud-datastore

我尝试创建一个查询,我可以通过某种连接表从用户那里获取项目。当我尝试这个时,我得到一个Nullpointer异常,但我不明白为什么;

这些是我的班级 的项目

@Entity
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key projectId;
private String projectName;
private User projectLeader;
private ArrayList<Task> tasks;

public Project(){
    tasks = new ArrayList<Task>();
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

public Key getProjectId() {
    return projectId;
}

public void setProjectId(Key projectId) {
    this.projectId = projectId;
}

public Project(User pLeader){
    tasks = new ArrayList<Task>();
}

public User getProjectLeader() {
    return projectLeader;
}

public void setProjectLeader(User projectLeader) {
    this.projectLeader = projectLeader;
}

public ArrayList<Task> getTasks() {
    return tasks;
}

public void setTasks(ArrayList<Task> tasks) {
    this.tasks = tasks;
}

public void addTask(Task newTask){
    tasks.add(newTask);
}

public void removeTask(Task removeableTask){
    tasks.remove(removeableTask);
}
}

ProjectUser

@Entity
public class ProjectUser {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Key projectUserId;
Key user;
Key project;

public ProjectUser(Key u, Key p){
    this.user = u;
    this.project = p;
}

public Key getProjectUserId() {
    return projectUserId;
}

public void setProjectUserId(Key projectUserId) {
    this.projectUserId = projectUserId;
    System.out.println("");
}

public Key getUser() {
    return user;
}

public void setUser(Key user) {
    this.user = user;
}

public Key getProject() {
    return project;
}

public void setProject(Key project) {
    this.project = project;
}
}

我的数据存储区中的记录

对于项目用户 ProjectUser Record

对于该项目 Project Record

对于用户 enter image description here

正如您在projectuser记录中看到的那样,有一个链接到现有用户的项目。我尝试使用以下方法获取项目,但之后我在此行获取nullpointer:List<Project> userProjects = q.getResultList();

public List<Project> getUserProjects(User u) {
    EntityManager em = EMFService.get().createEntityManager();
    Query q = em.createQuery("SELECT p FROM Project p, ProjectUser pu WHERE pu.user = :User AND p.projectId = pu.project");
    q.setParameter("User", u.getUserId());
    List<Project> userProjects = q.getResultList();
    return userProjects;
}

所以我的问题是:为什么我得到一个nullpointer,我该怎么做才能解决这个问题?

提前致谢:)

1 个答案:

答案 0 :(得分:1)

Google / GAE数据存储区不是关系数据库,不支持连接的概念。虽然您可以使用JPA的一部分来处理数据存储区,但是与加入有关的JPA的任何部分都不起作用。 您可能需要查看专为GAE数据存储构建的Objectify,并允许您根据实际数据模型进行编码。