我正在用 GAE 和 Java 为大学做一个网络应用程序。我创建了两个实体:用户和汽车。每个用户可以拥有多于一辆车。所以我创建了一个与汽车的关系实体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;
}
e 创建汽车的代码:
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;
}