Getting entity with a join table GAE

106 views Asked by At

I trying to create a query where i can get the projects from a user via some sort of join table. When I try this I get a Nullpointer exception, but I don't understand why;

These are my classes
Project

@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;
}
}

The records in my datastore

For the project user ProjectUser Record

For the project Project Record

For the user enter image description here

As you can see in the projectuser record there is a project linked to an existing user. I try to get the project with the following method but then I get the nullpointer at this line: 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;
}

So my question is: Why am I getting a nullpointer and what can I do to solve this?

Thanks in advance :)

1

There are 1 answers

2
Gabriel On

Google/GAE Datastore is not a relational database, and does not support the concept of join. Though you can use part of JPA to work with the datastore, any part of JPA that has anything to do with joining will not work. You may want to look into Objectify, which is build specifically for GAE Datastore, and lets you code according to the actual data model.