3
나는 끈기와 목표에 젖어 있습니다. 상위 키 할당에 대한 지침이 필요합니다. 내 구체적인 질문은 모두 대문자입니다. 감사. 는 (샘플 모델은 아래 APPUSER 및 비디오를 포함하는 아이디어는 유튜브처럼, 사용자가 그/그녀 속하는 동영상을 만듭니다..)Objectify 부모 키에 값을 할당하는 방법
@Entity
class Video{
// QUESTION 1: SHOULD THIS CLASS HAVE ONLY 1 KEY FIELD IF I WANT A
PARENT RELATIONSHIP WITH AppUser, AND TYPE IS Key<AppUser> ?
@Parent Key<AppUser> owner;
@Id private Long id;
protected Video(){}
protected Video(User u){ // GAE User object
AppUser au = ofy().load().type(AppUser.class).filter("userId",u.getUserId()).first().get();
// QUESTION 2: WHICH WAY IS RIGHT (TO ASSIGN PARENT KEY)?
this.owner = Key.create(au.getKey(),AppUser.class,au.getId());
// or:
// owner = au.getKey();
// or:
// owner = au;
}
}
@Entity
public class AppUser {
@Id private String userId;
// QUESTION 3: DO ALL CLASSES REQUIRE A KEY FIELD?
private Key<AppUser> key;
protected AppUser(){}
protected AppUser(User u){// GAE User object
this.userId = u.getUserId();
}
public String getId(){
return userId;
}
public Key<AppUser> getKey(){
// QUESTION 4: IS THIS THE CORRECT WAY TO RETURN THE KEY?
// WOULD THAT IMPLY I NEED TO EXPLICITLY ASSIGN A VALUE TO FIELD key?
return this.key;
// THE ALTERNATIVE WOULD BE TO CREATE A KEY
AND RETURN IT RIGHT? (THEN I CAN EXCLUDE FIELD key?)
// return Key.create(AppUser.class, userId);
}
}
AppUser au = ofy() .load(). (AppUser.class) .filter ("userId", u.getUserId()). first(). get() ; '비디오 생성자에서 테스트하기가 어려울 것입니까? 어쩌면 AppUser를 Video 생성자에 전달하는 것이 좋습니다. –