与Google应用引擎的实体关系 [英] Entity Relationship with Google app engine
问题描述
car = new Entity(Car,plate.hashCode(),user.getKey());
的关系实体,我的问题是在搜索汽车靠钥匙。我怎样才能做到这一点?因为我的代码 Entity car = getSingleCar(plate);
return always null。这是我的用户代码: public static void createOrUpdateCustomer(String name,String surname,
String phone,String地址,字符串城市,字符串状态,
字符串zip,字符串电子邮件,字符串用户名,字符串密码){
实体客户= getSingleUser(用户名);
if(customer == null){
customer = new Entity(User,username.hashCode());
customer.setProperty(name,name);
customer.setProperty(surname,姓氏);
customer.setProperty(phone,phone);
customer.setProperty(地址,地址);
customer.setProperty(city,city);
customer.setProperty(state,state);
customer.setProperty(zip,zip);
customer.setProperty(email,email);
customer.setProperty(用户名,用户名);
customer.setProperty(password,password);
} else {
System.out.println(esistegià);
if(name!= null&&!。equals(name)){
customer.setProperty(firstName,name); (姓氏)= null&&!。equals(surname)){
customer.setProperty(lastName,surname);
}
。 (phone)= null&&!。equals(phone)){
customer.setProperty(phone,phone);
}
if (地址!= null&&!。equals(address)){
customer.setProperty(address,address);
}
。 (城市!= null&&!。equals(city)){
customer.setProperty(city,city);
}
。 (state!= null&&!。equals(state)){
customer.setProperty(state,state);
}
。 (zip!= null&&!。equals(zip)){
customer.setProperty(zip,zip);
}
if (电子邮件!= null&&!。equals(email)){
customer.setProperty(email,email);
}
。 (用户名!= null&&!。equals(username)){
customer.setProperty(username,username);
}
。 (密码!= null&&!。equals(password)){
customer.setProperty(password,password);
}
。
}
}
Util.persistEntity(customer);
}
public static Entity getSingleUser(String username){
Key key = KeyFactory.createKey(User,username.hashCode());
返回Util.findEntity(key);
}
public static Iterable< Entity> getUser(字符串用户名){
Iterable< Entity>实体= Util.listEntities(用户,用户名,用户名);
返回实体;
}
public static Iterable< Entity> getUserById(String id){
Iterable< Entity> entities = Util.listEntities(User,ID,id);
返回实体;
}
public static Iterable< Entity> getAllUsers(){
Iterable< Entity> entities = Util.listEntities(User,null,null);
返回实体;
}
创建Car的代码:
public static Entity createOrUpdateCar(String username,String plate,String model,String revision,String service,String brand,String motor){
Entity user = User.getSingleUser(用户名);
System.out.println(user.getKey());
实体车= getSingleCar(板);
if(car == null&& user!= null){
System.out.println(ènull);
car = new实体(Car,plate.hashCode(),user.getKey());
car.setProperty(盘子,盘子);
car.setProperty(user,username);
car.setProperty(model,model);
car.setProperty(revision,revision);
car.setProperty(service,service);
car.setProperty(mechanic,null);
car.setProperty(品牌,品牌);
car.setProperty(电机,电机);
car.setProperty(riparazione,null);
} else {
System.out.println(id+ car.getProperty(plate));
}
Util.persistEntity(car);
回车;
}
public static Entity getSingleCar(String plate){
Key key = KeyFactory.createKey(Car,plate.hashCode());
返回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);
返回实体;
}
public static Iterable< Entity> getAllCars(){
Iterable< Entity>实体= Util.listEntities(Car,null,null);
返回实体;
}
发现你的代码总是为空的原因 Entity car = getSingleCar(plate)
;是你在使用这个查询的错误键。
创建一个新的Car实体时,使用键(Car,plate。 hashCode(),user.getKey());
它有3个部分 - kind,id,祖先关键字
但是在查询汽车时,你没有添加祖先密钥,因此你最终使用一个不匹配的密钥进行查询 Key key = KeyFactory.createKey(Car,plate.hashCode()); $ c $相反,你应该使用一个同样具有祖先键的键
Key key = KeyFactory.createKey(user.getKey(), Car,plate.hashCode());
请更改您的查询方法以获取此信息。 阅读此类似文章以更好地理解实体构造函数详细信息和KeyFactory方法。
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;
}
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.
这篇关于与Google应用引擎的实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!