如何获取内部实体列表?

时间:2016-07-20 06:28:19

标签: android google-app-engine google-cloud-endpoints objectify google-cloud-datastore

我有两个班级 - 员工和工作 - 作为一个领域

public class Employee {
   private Long id;
   private String name;
   private List<Job> jobList = new ArrayList<>();
   private Week week;

   public Employee() {
   }

   public Employee(String name) {
       this.name = name;
       jobList.add(new Job("Haircut", 30, 300));
       week = Week.init();
   }
//Getters and setters for all fields.
}

public class Job {
    private Long id;
    private String title;
    private int duration;
    private int price;

    public Job() {
    }

    public Job(String title, int duration, int price) {
        this.duration = duration;
        this.price = price;
        this.title = title;
    }
//Getters and setters for all fields.
}

并作为后端

@Entity
public class Employee {
    @Id
    private Long id;
    private String name;
    private List<Job> jobList = new ArrayList<>();
    private Week week;

    public Employee() {
    }
    //Getters and setters for all fields.
}

@Entity
public class Job {
    @Id
    private Long id;
    private String title;
    private int duration;
    private int price;

    public Job() {
    }
    //Getters and setters for all fields.
}

在OfyService注册

public class OfyService {

    static {
        factory().register(Employee.class);
        factory().register(Job.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }

    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }
}

我的EmployeeEndpoint有两种方法:

@ApiMethod(
    name = "list",
    path = "employee",
    httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Employee> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
    limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
    Query<Employee> query = OfyService.ofy().load().type(Employee.class).limit(limit);
    if (cursor != null) {
        query = query.startAt(Cursor.fromWebSafeString(cursor));
    }
    QueryResultIterator<Employee> queryIterator = query.iterator();
    List<Employee> employeeList = new ArrayList<Employee>(limit);
    while (queryIterator.hasNext()) {
        employeeList.add(queryIterator.next());
    }
    return CollectionResponse.<Employee>builder().setItems(employeeList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}

@ApiMethod(
    name = "jobList",
    path = "employee/{id}/jobList",
    httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Job> jobList(@Named("id") Long employeeId) {
    int limit = DEFAULT_LIST_LIMIT;
    Query<Job> query = OfyService.ofy().load().type(Job.class).limit(limit);
    QueryResultIterator<Job> queryIterator = query.iterator();
    List<Job> jobList = new ArrayList<>(limit);
    while (queryIterator.hasNext()) {
        jobList.add(queryIterator.next());
    }
    return CollectionResponse.<Job>builder().setItems(jobList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}

我可以使用 list()方法在服务器上看到员工,并且员工中列出了一个Job:

200

{
    "items": [
   {
   "id": "5732568548769792",
   "name": "Sophie",
   "jobList": [
      {
       "title": "Haircut",
       "duration": 30,
       "price": 300
      }
   ],
...
}

但是当我尝试通过id = 5732568548769792的 jobList()方法获取此作业时,它什么都不返回:

200

{
 "kind": "employeeApi#resourcesItem",
 "etag": "\"eEm63BNCBzxEAy3aqxetQdspGKA/FzFIL_vxOIIBu5chWZYooI-ArvQ\""
}

如果我用

指定祖先
Query<Job> query = OfyService.ofy().load().type(Job.class).ancestor(Key.create(Employee.class, employeeId)).limit(limit);
jobList()中的

结果不会改变。

我应该在上面的代码中更改哪些内容才能获得所需的职位列表?

0 个答案:

没有答案