간단한 GAE 서비스를 구현하려고합니다. 특수 학생 본인과 카테고리 주체가 있습니다. 각 학생에게는 하나 이상의 카테고리를 연결할 수 있습니다. Objectify를 사용하여이 관계를 어떻게 만들 수 있습니까? 감사합니다.Objectify 및 관계
편집 : 이것은 내 코드입니다. 그것은 유효합니까?
@Entity
public class Studente {
static long nextID = 17;
public static Key<Studente> key(long id) {
return Key.create(Studente.class, id);
}
List<Key<Categoria>> categorie;
public Studente() {}
@Id Long id;
@Index String nome;
@Index String cognome;
@Index String username;
@Index String password;
public Studente(String nome, String cognome, String username, String password) {
this.nome=nome;
this.cognome=cognome;
this.username=username;
this.password=password;
categorie = new ArrayList<Key<Categoria>>();
}
public static long getNextID() {
return nextID;
}
public static void setNextID(long nextID) {
Studente.nextID = nextID;
}
public List<Key<Categoria>> getCategorie() {
return categorie;
}
public void setCategorie(List<Key<Categoria>> categorie) {
this.categorie = categorie;
}
public void addCategoria(Key<Categoria> k){
categorie.add(k);
}
}
, 샘플 작업 코드를 작성했습니다. 내 질문에 그것을 확인할 수 있습니까? – Frank
당신의 솔루션은 효과가 있지만 매우 편리하지는 않습니다. 'Ref'는'Key'보다 더 편리합니다 (링크 된 문서를보십시오). 또한 목록에'@ Index' 주석이 없어도 특정 카테고리의 학생을 쿼리 할 수 없습니다. 어떤 학생이 어떤 범주에 속하는지 만 알 수 있습니다. 모든 것을 제쳐두고 떠나면 솔루션이 작동합니다. 그렇습니다. –
각'Student'와'Category'에서 참조 목록을 사용하여 중개자 'StudentCategory' 엔티티가 없어도 똑같이 할 수 있습니다. 이렇게하면 추가 코드와 비용이 절약됩니다. –