1

간단한 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); 
    } 
} 

답변

0

두 엔티티에 대한 색인 된 참조가있는 세 번째 엔티티를 제안 할 것입니다. 이렇게하면 카테고리의 모든 학생 또는 학생의 모든 카테고리를 쉽게 쿼리 할 수 ​​있습니다.

@Entity 
public class Student { /*...*/ } 

@Entity 
public class Category { /*...*/ } 

@Entity 
public class StudentCategory { 
    @Id 
    private Long id; 

    @Index 
    private Ref<Student> student; 

    @Index 
    private Ref<Category> category; 

    /*...*/ 
} 

GAE 응용 프로그램에서도 비슷한 설정을 사용하고 있습니다.

See documentation of Ref<?>.

+0

, 샘플 작업 코드를 작성했습니다. 내 질문에 그것을 확인할 수 있습니까? – Frank

+0

당신의 솔루션은 효과가 있지만 매우 편리하지는 않습니다. 'Ref'는'Key'보다 더 편리합니다 (링크 된 문서를보십시오). 또한 목록에'@ Index' 주석이 없어도 특정 카테고리의 학생을 쿼리 할 수 ​​없습니다. 어떤 학생이 어떤 범주에 속하는지 만 알 수 있습니다. 모든 것을 제쳐두고 떠나면 솔루션이 작동합니다. 그렇습니다. –

+0

각'Student'와'Category'에서 참조 목록을 사용하여 중개자 'StudentCategory' 엔티티가 없어도 똑같이 할 수 있습니다. 이렇게하면 추가 코드와 비용이 절약됩니다. –

1

모든 Category ID를 보유 (또는 키)이 Student에서 무티 값 인덱스 필드를 만듭니다

@Entity 
public class Category { 
    @Id 
    public Long id; // auto-generated Id of the Category 

} 

@Entity 
public class Student { 
    @Id 
    public Long id; // auto-generated Id of the Student 

    @Index 
    public List<Long> categories; // put Category Ids into this list 
} 
당신이 검색 할 수 있도록

인덱스 필드, 쿼리 필터에 사용할 수 있습니다 특정 범주에 속하는 학생.

+0

답변을받는 동안 샘플 코드를 작성했습니다. 내 질문에 그것을 확인할 수 있습니까? 답변을 얻으려면 – Frank