我正在使用GAE和Java为大学做一个Web应用程序。我创建了两个实体:User和Car。每个用户可以拥有一辆以上的汽车。所以我用汽车创建了一个关系实体car= new Entity("Car",plate.hashCode(), user.getKey());
我的问题在于按键搜索汽车。我怎样才能做到这一点?因为我的代码Entity car= getSingleCar(plate);
总是返回null。这是我的用户代码:
public static void createOrUpdateCustomer(String name, String surname,
String phone, String address, String city, String state,
String zip, String email, String username, String password) {
Entity customer = getSingleUser(username);
if (customer == null) {
customer = new Entity("User", username.hashCode());
customer.setProperty("name", name);
customer.setProperty("surname", surname);
customer.setProperty("phone", phone);
customer.setProperty("address", address);
customer.setProperty("city", city);
customer.setProperty("state", state);
customer.setProperty("zip", zip);
customer.setProperty("email", email);
customer.setProperty("username", username);
customer.setProperty("password", password);
} else {
System.out.println("esiste già");
if (name != null && !"".equals(name)) {
customer.setProperty("firstName", name);
}
if (surname != null && !"".equals(surname)) {
customer.setProperty("lastName", surname);
}
if (phone != null && !"".equals(phone)) {
customer.setProperty("phone", phone);
}
if (address != null && !"".equals(address)) {
customer.setProperty("address", address);
}
if (city != null && !"".equals(city)) {
customer.setProperty("city", city);
}
if (state != null && !"".equals(state)) {
customer.setProperty("state", state);
}
if (zip != null && !"".equals(zip)) {
customer.setProperty("zip", zip);
}
if (email != null && !"".equals(email)) {
customer.setProperty("email", email);
}
if (username != null && !"".equals(username)) {
customer.setProperty("username", username);
}
if (password != null && !"".equals(password)) {
customer.setProperty("password", password);
}
}
Util.persistEntity(customer);
}
public static Entity getSingleUser(String username) {
Key key = KeyFactory.createKey("User", username.hashCode());
return Util.findEntity(key);
}
public static Iterable<Entity> getUser(String username) {
Iterable<Entity> entities = Util.listEntities("User", "username", username);
return entities;
}
public static Iterable<Entity> getUserById(String id) {
Iterable<Entity> entities = Util.listEntities("User", "ID", id);
return entities;
}
public static Iterable<Entity> getAllUsers() {
Iterable<Entity> entities = Util.listEntities("User", null, null);
return entities;
}
创建汽车的代码:
public static Entity createOrUpdateCar(String username, String plate, String model, String revision, String service, String brand, String motor){
Entity user= User.getSingleUser(username);
System.out.println(user.getKey());
Entity car= getSingleCar(plate);
if(car==null && user!=null){
System.out.println("è null");
car= new Entity("Car",plate.hashCode(), user.getKey());
car.setProperty("plate", plate);
car.setProperty("user", username);
car.setProperty("model", model);
car.setProperty("revision", revision);
car.setProperty("service", service);
car.setProperty("mechanic", null);
car.setProperty("brand", brand);
car.setProperty("motor", motor);
car.setProperty("riparazione", null);
}else {
System.out.println("id"+car.getProperty("plate"));
}
Util.persistEntity(car);
return car;
}
public static Entity getSingleCar(String plate) {
Key key = KeyFactory.createKey("Car", plate.hashCode());
return Util.findEntity(key);
}
public static Entity getSingleCarByKey(Key key) {
return Util.findEntity(key);
}
public static Iterable<Entity> getCar(String plate) {
Iterable<Entity> entities = Util.listEntities("Car", "plate", plate);
return entities;
}
public static Iterable<Entity> getAllCars() {
Iterable<Entity> entities = Util.listEntities("Car", null, null);
return entities;
}
答案 0 :(得分:1)
在看到您的代码后,我发现您的代码Entity car= getSingleCar(plate)
始终为空的原因;就是你在这个查询中使用了错误的密钥。
创建新的Car实体时,您使用的键("Car",plate.hashCode(), user.getKey());
包含3个部分 - kind,id,ancestor key
但是在查询汽车时,您没有添加祖先密钥,因此您最终使用不匹配的密钥进行查询Key key = KeyFactory.createKey("Car", plate.hashCode());
相反,你应该使用一个也具有祖先密钥Key key = KeyFactory.createKey(user.getKey(), "Car", plate.hashCode());
的密钥
请更改您的查询方法以使其具有此功能。 Read this similar post更好地理解实体构造函数详细信息和KeyFactory方法。