0

I have an Entity called User. I have another Entity Movies.

I would like that the User has a List. However I have lots of exception with my code. Can you explain me how to do this ?

Thanks

andrel
  • 1,144
  • 11
  • 25
g123k
  • 3,748
  • 6
  • 42
  • 45

3 Answers3

1

An example of the relationship between User and Movie can look sometling like this:

User class:

import java.util.Set;
import com.google.appengine.api.datastore.Key;

public class User {

    @Persistent
    private Set<Key> ownsMovies;

    public void addMovie(Movie movie) {
        // We remember to maintain the relation both ways.
        ownsMovies.add(movie.getKey());
        movie.getOwners().add(getKey());
    }

    public void removeMovie(Movie movie) {
        // We remember to maintain the relation both ways.
        ownsMovies.remove(movie.getKey());
        movie.getOwners().remove(getKey());
    }
}

Movie class.

import java.util.Set;
import com.google.appengine.api.datastore.Key;

public class Movie {

    @Persistent
    private Set<Key> owners;

}
andrel
  • 1,144
  • 11
  • 25
0

Maybe still of interest: there is another nice Stackoverflow answer which explains this pretty good using JDO or JPA.

Community
  • 1
  • 1
disco crazy
  • 31,313
  • 12
  • 80
  • 83
-2

There is no reference list property in appengine. Still you can use db.ListProperty(db.Key) which stores a list of any entity's keys.

models:

 class User(db.Model):
   movie_list=db.ListProperty(db.Key)

 class Movie(db.Model):
   name=db.StringProperty()

views:

  user=User()
  movies=Movie.gql("")#The Movie entities you want to fetch

  for movie in movies:
      user.movie_list.append(data)

/// Here movie_list stores the keys of Data entity

Data.get(user.movie_list) will get all the Movie entities whose key are in the data_list attribute

Abdul Kader
  • 5,781
  • 4
  • 22
  • 40