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
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
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;
}
Maybe still of interest: there is another nice Stackoverflow answer which explains this pretty good using JDO or JPA.
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