2011-01-13 10 views
0

私はUserというエンティティを持っています。 私は別のエンティティ映画を持っています。エンティティにエンティティのリストを追加するにはどうすればよいですか? (AppEngine Java)

私はユーザーにリストがあることを希望します。 しかし、私は自分のコードで多くの例外があります。 これを行う方法を教えてもらえますか?

ありがとう

+1

おそらくコードを含めると、例外が発生している可能性があります。 –

答えて

-2

appengineには参照リストのプロパティはありません。それでも、エンティティのキ​​ーのリストを格納するdb.ListProperty(db.Key)を使用できます。

モデル:

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

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

ビュー:

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

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

///ここmovie_list取得するデータエンティティ

Data.get(user.movi​​e_list)のキーを格納し、すべての作品キーがdata_list属性にあるエンティティ

+2

AppEngine * java * – jamie

0

多分まだ興味があります:another nice Stackoverflow answerこれはJDOまたはJPAを使用してこれを説明しています。

Userクラス:

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クラス

1

UserMovieとの関係の一例は次のようにsometling見ることができます。

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

public class Movie { 

    @Persistent 
    private Set<Key> owners; 

} 
関連する問題