1

제공된 상위 키를 사용하여 엔티티를 필터링하여 Google 데이터 스토어의 엔티티를 가져 오는 코드를 작성했습니다. 코드를 실행할 때 java.lang.IllegalArgumentException이 표시됩니다.상위 키가있는 필터 엔티티 구체화

부모 키를 만드는 방법에 문제가 있다는 것을 알고 있습니다.이 유스 케이스의 부모 키를 효과적으로 만드는 법을 안내해 주시겠습니까?

내가 Myservice.java 줄에 아래의 예외를 점점 오전 8

Method threw 'java.lang.IllegalArgumentException' exception 
- Class hierarchy for class java.lang.Class has no @Entity annotation 

하는 App Engine의 v1.9.36는 객관화의 v5.1.7, JDK의 V1.7 아래

는 샘플 코드

입니다
import com.googlecode.objectify.Key; 
import com.googlecode.objectify.annotation.Cache; 
import com.googlecode.objectify.annotation.Entity; 
import com.googlecode.objectify.annotation.Id; 

    @Entity 
    @Cache 
    public class ParentEntity { 

     @Id 
     Long id; 

     String name; 

     String value; 

     public static Key<ParentEntity> getKey(Long id){ 
      return Key.create(ParentEntity.class, id); 
     } 

     public Long getId() { 
      return id; 
     } 

     public void setId(Long id) { 
      this.id = id; 
     } 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getValue() { 
      return value; 
     } 

     public void setValue(String value) { 
      this.value = value; 
     } 
    } 

다른 개체 클래스

당신이 다음에 부모 개체를 삽입 할 때 지금

public String getWebSafeKey(){ 
        return Key.create(ParentEntity.class, id).getString(); 
      } 

:

public static Key<ParentEntity> getKey(Long id){ 
      return Key.create(ParentEntity.class, id); 
     } 

에 : 객관화를 사용

import com.googlecode.objectify.Key; 
import com.googlecode.objectify.annotation.Cache; 
import com.googlecode.objectify.annotation.Entity; 
import com.googlecode.objectify.annotation.Id; 
import com.googlecode.objectify.annotation.Parent; 

@Entity 
@Cache 
public class ChildEntity { 

    @Id 
    Long id; 

    @Parent Key<ParentEntity> application; 

    String city; 

    public static Key<ChildEntity> getKey(Long id){ 
     return Key.create(Key.create(ParentEntity.class), ChildEntity.class, id); 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public Key<ParentEntity> getApplication() { 
     return application; 
    } 

    public void setApplication(Key<ParentEntity> application) { 
     this.application = application; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 
} 

ServiceLaver 당신의 ParentEntity의 방법 실체를

import java.util.List; 
import com.googlecode.objectify.ObjectifyService; 

public class MyService{ 

    public List<ChildEntity> filterByID(Long id){ 
     return ObjectifyService.ofy().load().type(ChildEntity.class) 
      .filterKey(ChildEntity.getKey(id)).first().now(); 
    } 
} 
+2

을 명세서에 'Key.create (ParentEntity.class), ChildEntity.class, id)를 리턴하고,'부분'Key.create (ParentEntity. 클래스)'는'Key.create (ParentEntity.class, parentId)'처럼 id도 가져야합니다. 조상 키에 와일드 카드를 사용하는 필터는 만들 수 없습니다. 조상 쿼리로 다른 방법으로 만 작동합니다. – konqi

답변

3

변화를 가져올 수 응답은 당신에게 줄 것이다 해당 상위 항목의 웹 안전 키. 삽입 된 상위 엔티티에 액세스하려면이 웹 보안 키를 사용하십시오.

public List<ChildEntity> filterByID(Long id){ 
     return ObjectifyService.ofy().load().type(ChildEntity.class) 
      .filterKey(ChildEntity.getKey(id)).first().now(); 
    } 

사람 : : 그 변경 후

public List<ChildEntity> filterByID(String parentWebSafeKey){ 
     return ObjectifyService.ofy().load().type(ChildEntity.class) 
      .ancestor(Key.create(parentWebSafeKey)).first().now(); 
    } 

사용하여 자식 개체를 만드는 동안 ParentEntity와 ChildEntity 사이의 관계를하는 것을 잊지 마세요 :

child_entity.application = Ref.create(parent_entity_key);