Reputation: 740
I'm doing a web application for university with GAE and Java. I created two entity: User and Car. Each user can have plus than one car. So i created a relationship entity with car car= new Entity("Car",plate.hashCode(), user.getKey());
My problem is in the searching of the car by key. How can i do this? because my code Entity car= getSingleCar(plate);
return always null. This is my code for User:
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 the code for create a Car:
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;
}
Upvotes: 0
Views: 259
Reputation: 4779
After seeing your code, I find that the reason why you are always getting null for your code Entity car= getSingleCar(plate)
; is that you are using the wrong key for this query.
When creating a new Car entity you use the key ("Car",plate.hashCode(), user.getKey());
which has 3 parts - kind, id, ancestor key
But while querying for car, you are not adding the ancestor key and hence you end up using a non-matching key for the query Key key = KeyFactory.createKey("Car", plate.hashCode());
Instead you should be using a key which also has the ancestor key Key key = KeyFactory.createKey(user.getKey(), "Car", plate.hashCode());
Please change your query method to have this. Read this similar post to understand entity constructor details and KeyFactory methods better.
Upvotes: 1