0

我正在用 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;
}
4

1 回答 1

1

看到您的代码后,我发现您的代码总是为 null 的原因Entity car= getSingleCar(plate);是您为此查询使用了错误的键。

在创建新的 Car 实体时,您使用("Car",plate.hashCode(), user.getKey());具有 3 个部分的键 - 种类、ID、祖先键

但是在查询汽车时,您没有添加祖先键,因此您最终使用不匹配的键进行查询Key key = KeyFactory.createKey("Car", plate.hashCode());

相反,您应该使用一个也具有祖先键的键Key key = KeyFactory.createKey(user.getKey(), "Car", plate.hashCode()); 。请更改您的查询方法以拥有它。阅读这篇类似的文章,以更好地了解实体构造函数的详细信息和 KeyFactory 方法。

于 2013-06-26T11:31:56.620 回答