我编写了一件代码,该代码通过用提供的父键过滤实体来从Google数据存储中获取实体。当我运行代码时,我将获得java.lang.IllegalArgumentException
。
我知道问题在于我创建父键的方式,您能否指导我如何有效地为此用例创建父键?
我在Myservice.java
行8
Method threw 'java.lang.IllegalArgumentException' exception
- Class hierarchy for class java.lang.Class has no @Entity annotation
appengine 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;
}
}
另一个实体类
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;
}
}
使用Objectifify获取实体的Servicelaver
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();
}
}
更改您的寄养方法:
public static Key<ParentEntity> getKey(Long id){
return Key.create(ParentEntity.class, id);
}
to:
public String getWebSafeKey(){
return Key.create(ParentEntity.class, id).getString();
}
现在,当您插入父实体时,然后在响应中会给您 websafe键该父实体。只要您想访问此插入的父实体。
此之后:
public List<ChildEntity> filterByID(Long id){
return ObjectifyService.ofy().load().type(ChildEntity.class)
.filterKey(ChildEntity.getKey(id)).first().now();
}
to:
public List<ChildEntity> filterByID(String parentWebSafeKey){
return ObjectifyService.ofy().load().type(ChildEntity.class)
.ancestor(Key.create(parentWebSafeKey)).first().now();
}
使用:
创建子体实体时,请不要忘记在寄养和儿童之间建立关系:child_entity.application = Ref.create(parent_entity_key);