您有两个相关的实体:客户和汽车.每个客户可以拥有多辆汽车
这是实体的摘要视图:
public class Customer
{
//Inner classes for partial loads
public static class NoCars{}
@Id protected String id;
private String fullName;
@Load(unless=NoCars.class) private List<Ref<Car>> cars;
}
public class Car
{
@Id private Long id;
private String makeAndModel;
private String plateNumber;
}
Run Code Online (Sandbox Code Playgroud)
这是一种从数据存储区和他拥有的所有汽车中检索客户的方法:
public Customer getCustomer(@Named("id") String customerId)
{
Customer customer = ofy().load().type(Customer.class).id(customerId).now();
if (customer==null)
throw new NotFoundException("customer not found");
else
return customer;
}
Run Code Online (Sandbox Code Playgroud)
endpoints.sh无法实现这一点,因为不支持List <Ref<Car>>返回类型Customer中包含的类型,但我发现这个有趣的解决方法:
我创建了CustomerPOJO类
public class CustomerPOJO …Run Code Online (Sandbox Code Playgroud)