使用父键对象化过滤器实体
Objectify filter entities with parent key
我编写了一段代码,通过使用提供的父键过滤实体,从 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;
}
}
另一个实体class
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;
}
}
使用 Objectify 获取实体的 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();
}
}
更改您的 ParentEntity 的方法:
public static Key<ParentEntity> getKey(Long id){
return Key.create(ParentEntity.class, id);
}
至:
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();
}
收件人:
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);
我编写了一段代码,通过使用提供的父键过滤实体,从 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;
}
}
另一个实体class
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;
}
}
使用 Objectify 获取实体的 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();
}
}
更改您的 ParentEntity 的方法:
public static Key<ParentEntity> getKey(Long id){
return Key.create(ParentEntity.class, id);
}
至:
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();
}
收件人:
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);