使用父键过滤实体 [英] Objectify filter entities with parent key
问题描述
我写了一段代码,通过使用提供的父键过滤实体,从google数据存储中提取实体。当我运行代码时,我得到 java.lang.IllegalArgumentException
。
我知道问题在于我创建父键的方式,请你指导我如何有效地为这个用例创建一个父键?
我在 Myservice.java
第8行找到以下异常:
方法抛出'java.lang.IllegalArgumentException'异常
- 类java.lang.Class的类层次结构没有@Entity注释
Appengine v1.9.36,
Objectify v5.1.7,
JDK v1.7 p>
以下是示例代码
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
公共类ParentEntity {
@Id
Long ID;
字符串名称;
字符串值;
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(){
返回值;
}
public void setValue(String value){
this.value = value;
$ / code $ / pre
另一个实体类
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键< ParentEntity>应用;
字符串城市;
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;
}
公钥< ParentEntity> getApplication(){
返回应用程序;
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();
$ / code $ / pre
解决方案更改您的ParentEntity的方法:
public static Key< ParentEntity> getKey(Long id){
return Key.create(ParentEntity.class,id);
到:
public String getWebSafeKey(){
return Key.create(ParentEntity.class,id).getString();
$ / code>
现在当你插入一个父实体时,它会给你该父实体的网络安全密钥。
更改后:
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);
I have written a piece of code that fetches entities from google datastore by filtering the entity with the supplied parent key. When I run the code I am getting java.lang.IllegalArgumentException
.
I know the problem is with the way I am creating the parent key, can you please guide me how to effectively create a parent key for this use case?
I am getting the below exception in Myservice.java
line 8
Method threw 'java.lang.IllegalArgumentException' exception
- Class hierarchy for class java.lang.Class has no @Entity annotation
Appengine v1.9.36,
Objectify v5.1.7,
JDK v1.7
Below is a sample code
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;
}
}
Another entity 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;
}
}
ServiceLaver that uses Objectify to fetch entities
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();
}
}
解决方案 change Your ParentEntity's method :
public static Key<ParentEntity> getKey(Long id){
return Key.create(ParentEntity.class, id);
}
to:
public String getWebSafeKey(){
return Key.create(ParentEntity.class, id).getString();
}
Now when you insert a parent entity then in response it will give you websafe key of that parent entity. Use this websafe key whenever you wants to access this inserted parent entity.
After that change:
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();
}
Don't forget to create relationship between ParentEntity and ChildEntity while creating child entity using:
child_entity.application = Ref.create(parent_entity_key);
这篇关于使用父键过滤实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!